Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve line breaks in a content editable div when using .text() - Jquery

Tags:

jquery

newline

I have a div which is having "contenteditable=true". When i enter text in it, and press enter the cursor goes to the next line by appending <br> in the previous line.

While I use the submit button, I use .text() to get the contents of the div. This strips of any html tags in the div, thus removing all the <br> tags. So no line breaks.

I wanna know, how do i preserve line breaks when submitting the form. Thanks for help...

like image 285
sanchitkhanna26 Avatar asked Apr 13 '13 08:04

sanchitkhanna26


1 Answers

Don't use Jquery for this, the .text() function removes white space. Use plain Javascript for this:

$(selector)[0].innerText = 'foo';
like image 73
Daan Avatar answered Sep 20 '22 00:09

Daan