Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .html() of all matched elements

.html() function on class selector ($('.class').html()) applies only to the first element that matches it. I'd like to get a value of all elements with class .class.

like image 475
Przemek Avatar asked Sep 22 '11 16:09

Przemek


People also ask

What is the use of jQuery each () function?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How do I get HTML using jQuery?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.

How do I get the html inside a div using jQuery?

With jQuery, you can use the . append() method to insert the specified HTML as the last child of the div container.

What does VAL () do in jQuery?

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined .


1 Answers

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/

like image 104
Samich Avatar answered Oct 12 '22 02:10

Samich