Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy div HTML to textarea but keep line breaks [duplicate]

I am working on a questionnaire application and one of the features is that the user can click on a question and and edit it. The problem I am having is converting a <br /> tag into a line break that my textarea will understand.

The save code looks as follows:

$('#questions').append("<div>"+$('textarea[name="question"]').val().replace(/\n/g, '<br />')+"</div");

This works perfectly! The code to convert it back looks like this:

$('textarea[name="question"]').val($('#questions').eq(1).html().replace(/<br\s*\/?>/mg,"\n"));

but for some reason the textarea will not accept the new lines and just bundles all of the text together.

How can I convert <br /> back into a new line feed that my textarea will understand?

like image 666
Robert E. McIntosh Avatar asked Oct 05 '22 11:10

Robert E. McIntosh


1 Answers

Try

$('textarea[name="question"]').val($('#questions').eq(1).html().replace(/\s*<br\s*\/?>\s*/g,"\n"));

Demo: Fiddle

like image 192
Arun P Johny Avatar answered Oct 10 '22 01:10

Arun P Johny