Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip HTML tags from div content using Javascript/jQuery?

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?

like image 611
user3418849 Avatar asked Mar 14 '14 07:03

user3418849


People also ask

How can we remove the HTML tags from the data?

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.

How do you cut text in HTML?

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.

Which method is used to get the content in HTML of the div element with ID courses in jQuery?

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.

Which jQuery function is used to change HTML of Div?

Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.


2 Answers

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.

like image 95
UmarKashmiri Avatar answered Oct 26 '22 08:10

UmarKashmiri


You can follow this example

var stringContent = "<p>Hi dear</p><p>Wish you a happy birthday</p>";

var text = $(stringContent).text();
like image 20
MusicLovingIndianGirl Avatar answered Oct 26 '22 08:10

MusicLovingIndianGirl