Delete tag but leave content - jQuery / Javascript

Today I’m a little tired and just can’t think how to do it simply. Basically, I have something similar to the following, and I want to cut out the html tags, but leave their contents:

<a href="#">some content</a> and more <a href="#"> and more content</a>

and actually return the following:

some content and more and more content

Any help, as always, is greatly appreciated!

EDIT: Thank you very much for the answers - there I followed the path of regular expression to complicate the situation! I actually finished using .text (), as suggested below, I used it before, but just for installation, I never retrieved it, and it worked better when a rather large object was returned! Thank you so much for all the suggestions :). I will answer the answer in 6 minutes.

+5
source share
5 answers

$ (selector) .text () should remove all html.

+4
source

This method is slightly longer, but perhaps more explanatory than the method contents().

$('a').each(function () {
    $(this).replaceWith($(this).html())
})

replaceWithaccepts html replacement. (not selectors)

+2
source

You can write

$(...).find('*')
      .replaceWith(function() { return this.childNodes });

I don't know how well this will handle nested tags.

+2
source

Maybe something like this:

jQuery.fn.stripTags = function() {
    return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );

};

Source: http://www.mail-archive.com/ jquery-en@googlegroups.com /msg18843.html

0
source

All Articles