Convert HTML encoded & lt; br / & gt; in html new line tag in jquery.text ()
I have text in a variable sometext
that I want to assign div
using $('div').text(sometext);
. The problem is that everyone <br/>
is sometext
turning into < br/ >
. I cannot use $('div').html()
because there is a lot of internal structure that I want to store in div
, so I can only use the function .text()
.
So, once I have passed the text over div
, is there a way to change the encoded tag <br/>
to a real line break in HTML? I mean something like this:
$('div').convertMyBRsToHTML();
source to share
The requested function should look like this:
$.fn.convertMyBRsToHTML = function(text){
this.text(text);
this.html(this.html().replace(/<br\s*\/>/g,'<br/>'));
return this;
}
Now you can use:
$(YOUR_SELECTOR).convertMyBRsToHTML("<b>Some</b> new text.<br />next line<br />and so on");
source to share