I have the HTML code
<span class="editableFalse" id="comments">
Lorem ipsum sit dolor amet
<br/>
Lorem ipsum sit dolor amet
</span>
<textarea id="textareaComments"></textarea>
Now I have to fetch the data from this span and set it to a Textarea below it in the same format, ie along with the newline character.
How to achieve that :
$("#textareaComments").val($("#comments").text());
The above line takes the content from the span but trims off the br tag. I dont get a newline character in the textarea. Any help.
Just use replace:
$("#textareaComments").val($("#comments").html().replace("<br>", "\n"));
var str = $("#comments").html();
var regex = /<br\s*[\/]?>/gi;
$("#textareaComments").val(str.replace(regex, "\n"));
Try the replaces Br with \n (new line)
And use .html() not .text()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With