Inserting text in teaxtarea cannot recognize new line
I am reading from a <td>
and inserting the text in a textarea but all texts appearing in one line. I want to keep the formatting in the te开发者_运维技巧xtarea as the text read from the column.
$("#text").text($.trim($(this).closest('tr').next('tr').find('.mytext).text()));
The text should be
my test is working with spaces but not with new line. <br />This is a new line.
But it seems that it not able to interprete the <br/>
. How can I make it read the <br/>
and display a new line in the textarea.
Your use of .text() will not get the <br />
since that is not text.
From the docs:
Get the combined text contents of each element in the set of matched elements, including their descendants.
Note it is only the text contents. I don't know what would happen if there were "real" line breaks in the text (as opposed to html ones) but this is where you want to look at your issue.
Replace the br-elements by a \n character, I think that should work.
Assuming that you're pulling raw HTML (with <br />
tags intact, rather than just the text), do as JNDPNT suggested, replace all <br />
tags with the \n
literal:
var input = 'my test is working with spaces but not with new line.<br />This is a new line.'; // This is a simulation of an input string
// Create the output (replace all <br /> tags with \n):
var output_text = input.replace(new RegExp('<br />', 'g'), '\n');
$('#output').val(output_text);
Here's a jsFiddle showing it working:
http://jsfiddle.net/xYHjn/
精彩评论