How to save line breaks in editable content div when using .text () - JQuery

I have a div that has contenteditable = true. When I enter text into it and press Enter, the cursor moves to the next line, adding <br> to the previous line.

While I am using the submit button, I am using .text() to get the contents of the div. These stripes are any html tags in the div, removing all <br> tags. Thus, no line is interrupted.

I want to know how I can save line breaks when submitting a form. Thanks for the help...

+4
source share
2 answers

Do not use jquery for this, the .text() function removes the white space. Use simple Javascript to do this:

 $(selector)[0].innerText = 'foo'; 
+5
source

Instead of .text() you can use .html() . This will not disable shortcuts. Your next problem will be trying to eliminate / convert tags.

You can learn more about .html() here: http://api.jquery.com/html .

+1
source

All Articles