How to toggle off document.execCommand("superscript") back to normal text

54 Views Asked by At

I am working on a custom text editor using a contenteditable div with basic formatting functionalities. I am using the command document.execCommand("superscript", false, undefined); to do superscript functionality, for example EXAMPLETEST. what command can i use to turn this back to normal text.

Ps. clicking the initial command again does not work

I have tried document.execCommand("superscript", false, undefined); and also document.execCommand("removeFormat", false, undefined); both dont work to toggle it off.

1

There are 1 best solutions below

0
Afzal K. On

superscript is a toggle function, it will only work if text is not there

so to get text to normal we will have to use the command again but making sure text is there, so there is a way to check if text is there in selection by using document.queryCommandState() method

if (document.queryCommandState("superscript")) {
  document.execCommand("superscript", false, undefined);  
} else {  
  // there is no text
}  

using removeFormat is not best option as it will remove all formatting

did that help ?