javascript - Keep specific HTML tags after string is passed into .text() function -
if have next string:
hello goodbye
can set string content of div maintain carriage returns?
jquery('<div class="mytooltip">').text(thestring);
atm, .text() returns this:
hello<br> <br> <br> <br> goodbye
i unable utilize .html() because of cross side scripting concerns.
could perhaps utilize regex on string maintain carraige returns beingness converted text?
if not regex, there option?
you can utilize replace convert'\n'
or '\n\r'
characters '<br />'
here example:
var mystring = 'some text \n'; var parsed= mystring.replace(/\n\r?/g, '<br />'); console.log(parsed);
fully working illustration here: http://jsfiddle.net/r6xrf5wh/1/
just inspect "the reddish box" see automatically inserted when using line break in input field.
<!doctype html> <html> <head> <style> #result { width: 200px; height: 200px; background-color: red; } </style> <script> window.parse = function() { var elm = document.getelementbyid('test'); var elmtext = elm.value; var parsed= elmtext.replace(/\n\r?/g, '<br />'); var elmresult = document.getelementbyid('result'); elmresult.innerhtml = parsed; }; </script> </head> <body> <textarea id="test" rows="4" cols="50"></textarea> <button type="button" onclick="parse()">click me!</button> <div id="result">x</div> </body> </html>
javascript html regex
No comments:
Post a Comment