jQuery .html() of all matched elements

You are selection all elements with class .class but to gather all html content you need to walk trough all of them:

var fullHtml;

$('.class').each(function() {
   fullHtml += $(this).html();
});

search items by containig text inside of it:

$('.class:contains("My Something to search")').each(function() {
   // do somethign with that
});

Code: http://jsfiddle.net/CC2rL/1/


I prefer a one liner:

var fullHtml = $( '<div/>' ).append( $('.class').clone() ).html();

You could map the html() of each element in a filtered jQuery selection to an array and then join the result:

           //Make selection
var html = $('.class')
          //Filter the selection, returning only those whose HTML contains string 
          .filter(function(){
              return this.innerHTML.indexOf("String to search for") > -1
          })
          //Map innerHTML to jQuery object
          .map(function(){ return this.innerHTML; })
          //Convert jQuery object to array
          .get()
          //Join strings together on an empty string
          .join("");

Documentation:

  • .filter()
  • .map()
  • .get()
  • .join()

$('.class').toArray().map((v) => $(v).html())