Search code examples
newlinecontenteditableparagraphexeccommand

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?


Solution

  • Try the execCommand "insertparagraph", e.g. :

    document.execCommand('insertParagraph',false,'id for new p')
    

    Alternatively you could handle the keydown event, catch keycode 13 (without modifiers) and then do whatever you like.

    Corrected