Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contenteditable paragraph tag on enter

I was wondering if there is an acceptable way to force all major browsers to insert paragraph tag instead of the default tag that they insert on pressing enter key when contentEditable is true.

As far as I know IE inserts p automatically. But Google Chrome inserts div tag and Firefox inserts br (WTF?!).

Thanks in advance!

like image 329
Dimitur Vulchanov Avatar asked Oct 12 '11 08:10

Dimitur Vulchanov


People also ask

How do you make an editable paragraph in HTML?

You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

What is the function of Contenteditable attribute?

The contenteditable attribute specifies whether the content of an element is editable or not.

Is Contenteditable safe to use?

Its totally secure. The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false.

How do you use Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.


1 Answers

you can use document.execCommand('formatBlock', false, 'p'); in event like keypress or keydown, etc. to use paragraphs after enter press. For example:

element.addEventListener('keypress', function(ev){
    if(ev.keyCode == '13')
        document.execCommand('formatBlock', false, 'p');
}, false);
like image 183
bartaxyz Avatar answered Oct 07 '22 22:10

bartaxyz