Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contentEditable areas::force paragraphs for new lines - cross browsers

Firefox breaks new lines with <br>, Chrome and Safari are breaking with <div>...</div> and Internet Explorer and Opera are using paragraphs <p>...</p> for new lines.

I am looking for a method to force each browser to create only paragraphs, when breaking to a new line.

I know ckeditor supports this function, but how can i realize it simply in my custom editor?

like image 803
PePe Avatar asked Jan 20 '26 12:01

PePe


1 Answers

For me was solution to add document.execCommand('formatBlock', false, 'p'); into a keypress event in the contenteditable div. For example:

element.addEventListener('keypress', function(ev){
    if(ev.keyCode == '13')
        document.execCommand('formatBlock', false, 'p');
}, false);

Hope it'll help someone. :)

like image 85
bartaxyz Avatar answered Jan 22 '26 03:01

bartaxyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!