I want to remove an HTML tag from string for example remove div,p,br,...
I'm trying to do this:
var mystring = "<div><p>this</p><p>is</p><p>my</p><p>text</p><p>sample</p><p> </p><p> </p></div>"
var html3 = $(mystring).text();
but the result is:
"thisismytextsample "
How can do it like : "this is my text sample"
Learning jQuery To remove HTML tags, use text() function which returns only the text content and ignores the HTML portion.
The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.
To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.
Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
You can get all p tag text in array and then join them with spaces:
$(mystring).find('p').map(function() {
return $(this).text();
}).toArray().join(' '));
Working Demo
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