5

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?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Robert E. McIntosh
  • 5,557
  • 4
  • 26
  • 35

2 Answers2

4

Try

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

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

You'll need to convert the <br>'s into HTML encoded newlines.

&#10; Line Feed and &#13; Carriage Return. This way you are actually parsing the new line ("\n") rather than displaying it as text.

New line in text area

Community
  • 1
  • 1
Jazzepi
  • 5,259
  • 11
  • 55
  • 81