How to strip HTML tags with jQuery?

Jquery

Jquery Problem Overview


I want to remove HTML tags from a string. For example assume we have the string:

 <p> example ive got a string</P>

How can I write a function that removes the <p><p> and returns just "example ive got a string"?

Jquery Solutions


Solution 1 - Jquery

Use the .text() function:

var text = $("<p> example ive got a string</P>").text();

Update: As Brilliand points out below, if the input string does not contain any tags and you are unlucky enough, it might be treated as a CSS selector. So this version is more robust:

var text = $("<div/>").html("<p> example ive got a string</P>").text();

Solution 2 - Jquery

The safest way is to rely on the browser TextNode to correctly escape content. Here's an example:

function encodeHTML(dirtyString) {
  var container = document.createElement('div');
  var text = document.createTextNode(dirtyString);
  container.appendChild(text);
  return container.innerHTML; // innerHTML will be a xss safe string
}

document.write( encodeHTML('<p>some <span>content</span></p>') );
document.write( encodeHTML('<script><p>some <span>content</span></p>') );

The thing to remember here is that the browser escape the special characters of TextNodes when we access the html strings (innerHTML, outerHTML). By comparison, accessing text values (innerText, textContent) will yield raw strings, meaning they're unsafe and could contains XSS.

If you use jQuery, then using .text() is safe and backward compatible. See the other answers to this question.

The simplest way in pure JavaScript if you work with browsers <= Internet Explorer 8 is:

string.replace(/(<([^>]+)>)/ig,"");

But there's some issue with parsing HTML with regex so this won't provide very good security. Also, this only takes care of HTML characters, so it is not totally xss-safe.

Solution 3 - Jquery

This is a example for get the url image, escape the p tag from some item.

Try this:

$('#img').attr('src').split('<p>')[1].split('</p>')[0]

Solution 4 - Jquery

If you want to keep the innerHTML of the element and only strip the outermost tag, you can do this:

$(".contentToStrip").each(function(){
  $(this).replaceWith($(this).html());
});

Solution 5 - Jquery

You can use the existing split function

One easy and choppy exemple:

var str = '<p> example ive got a string</P>';
var substr = str.split('<p> ');
// substr[0] contains ""
// substr[1] contains "example ive got a string</P>"
var substr2 = substr [1].split('</p>');
// substr2[0] contains "example ive got a string"
// substr2[1] contains ""

The example is just to show you how the split works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSadda-shutuView Question on Stackoverflow
Solution 1 - JqueryJonView Answer on Stackoverflow
Solution 2 - JquerySimon BoudriasView Answer on Stackoverflow
Solution 3 - JquerySocialSupport - SeonictechView Answer on Stackoverflow
Solution 4 - JqueryGinosuaveView Answer on Stackoverflow
Solution 5 - JqueryVicoManView Answer on Stackoverflow