Search code examples
jquerychaining

Using jQuery.not() chained with jQuery.html()


Is it possible to use jQuery.not() chained with jQuery.html()?

winner.not('a').html()

Where winner is a jQuery object/wrapped set, I am trying to return HTML with anchors removed.


Solution

  • .html() is going to return the innerHTML - which will include any A tags inside, you may be able to do something like this though:

    // clone the matched div:
    var copy = winner.clone();
    // remove all A tags from the DOM
    copy.find("a").remove();
    // get the html.
    var noanchors = copy.html();
    

    Also - If you wanted to get the text within the A still - but not the A itself - you could use:

    // for each A tag
    copy.find("a").each(function() {
      //insert the text within ourselves to the document directly before us.
      $(this).before($(this).text());
      // then delete ourselves
      $(this).remove();
    });
    

    Although that might actually get a little messy if the <a> has any other tags within it - it should illustrate the idea.