Friday 25 April 2014

Remove HTML tags from string but keep content.

Introduction:

Here I will explain how to remove specific HTML tag from string but keep content which is inside tag.

In JQuery we can remove element by remove()  function but how would we remove specific HTML tag from string.

Just need to use unwrap() function of JQuery.

Code:
$('p').contents().unwrap();



Example:






Suppose we want to remove all p page from string.

var temp='<div>Some text</div><p style="color:red">More text<span>here</span></p><p>Even more</p>';

var StrTemp=$(temp).find('p').contents().unwrap();

alert(StrTemp);



This will alert "<div>Some text</div>More text<span>here</span>Even more"
unwrap functino remove all p tag from string.