How to clear tags from a string using JavaScript
<div id="mydiv"> <p> <b><a href="mypage.html">This is an example<a>.</b> <br> This is another example. </p> </div> <script type="text/javascript"> var mystr = document.getElementById('mydiv').innerHTML; ..... </script> I want to clear all the tags and get the salt text,
mystr = "This is an example this is another example."; How can i do this?
Using innerText and textContent :
var element = document.getElementById('mydiv'); var mystr = element.innerText || element.textContent; innerTextsupported by all browsers, but FFtextContentsupported by all browsers, but IE
I only saw that the string would still contain line breaks. You can remove them with replace :
mystr = mystr.replace(/\n/g, ""); Update:
As @ Ε ime Vidas points out in his comment, it seems that you need to handle spaces a little differently to fix the line in IE:
mystr = mystr.replace(/\s+/g, ' ');