Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript document.execCommand remove formatBlock formatting?

If I format a piece of text on a page like this:

document.execCommand('formatBlock', false, 'h1');

What do I do to remove this formatting?

like image 837
Greg Avatar asked May 30 '12 06:05

Greg


3 Answers

I suppose document.execCommand('removeFormat',false,false) would do it?

Issuing document.execCommand('formatBlock', false, 'div') on the <h1>-block will remove the <h1>-tag and replace it with a <div>-tag 1. Would that be viable?

1 If you're not using IE that is

like image 156
KooiInc Avatar answered Oct 13 '22 15:10

KooiInc


I clear the effect of h1 using this:

document.execCommand('formatBlock', false, 'p');

You have changed its format to h1, so we can change it back to normal paragraph format in the same way.
If your put each paragraph in a <div> , you can also use this:

document.execCommand('formatBlock', false, 'div');

to set the format to the same as other blocks.

like image 3
Alsophila Avatar answered Oct 13 '22 14:10

Alsophila


I had the same problem where I need to delete the h1 tag wrapping my text.

What I did was get the parent node of the selected text:

var elem_parent_node = window.getSelection().getRangeAt(0).startContainer.parentNode;

And then check if it's nodeName is "H1"; if yes, then store the selected text in a selected_text variable and then delete the node itself:

elem_parent_node.remove();

Then,

document.execCommand('insertText', false, select_text);

like image 3
Paul Jhon Villaro Avatar answered Oct 13 '22 13:10

Paul Jhon Villaro