<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 tags, and get the salt text,
mystr = "This is an example this is another example.";
How can I do that?
Using innerText
and textContent
:
var element = document.getElementById('mydiv');
var mystr = element.innerText || element.textContent;
innerText
is supported by all browsers but FFtextContent
is supported by all browsers but IEDEMO
I just saw that the string will still contain line breaks. You might want to remove them with replace
:
mystr = mystr.replace(/\n/g, "");
Update:
As @Šime Vidas points out in his comment, it seems you have to handle the whites spaces a bit differently to fix the string in IE:
mystr = mystr.replace(/\s+/g, ' ');
Here is a different approach - remove the tags using replace with a regular expression:
document.getElementById('mydiv').innerHTML.replace(/\n|<.*?>/g,'')
Here is a fiddle
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