I had made one div
tag and stored its contents in a variable. If this tag contains p,b
or any other tags then it should be removed from string. How can I achieve this?
PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.
In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM. HTML tags are of two types opening tag and closing tag.
jQuery html() Method The html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element.
Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.
use the regular expression.
var regex = /(<([^>]+)>)/ig
var body = "<p>test</p>"
var result = body.replace(regex, "");
alert(result);
HERE IS THE DEMO
Hope this helps.
You can follow this example
var stringContent = "<p>Hi dear</p><p>Wish you a happy birthday</p>";
var text = $(stringContent).text();
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