Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over an array of selected jQuery Objects?

Tags:

jquery

How do you iterate through a group of selected jQuery objects so that you can perform jQuery functions on each object individually?

   
 <div class="foobar">abc</div>
 <div class="foobar">123</div>
 <div class="foobar">abc123</div>

I can select the group:

var foobarObjects = jQuery('.foobar')

But how would you go through each jQuery object in foobarObject and manipulate each one individually? I thought I could use jQuery().each but that only allows me to work with DOM objects, not jQuery objects. I also tried a for loop in conjunction with the jQuery().eq(i) function, but that seems to merge the items together.

like image 712
JayNCoke Avatar asked Oct 12 '09 18:10

JayNCoke


People also ask

How do I iterate an array of objects in jQuery?

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 iterate through select options in jQuery?

Iterate through <select> options using jQuery To traverse the <select> tag, a function has to be created which will return the text, value, or both (text and value) of the options in it. For this, each() method of jQuery will be used. Using this, elements can be traversed easily.

How do I iterate through a div in jQuery?

jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children() method is called to find all the child elements of the selected element.

How do you loop through an element with the same class in jQuery?

Answer: Use the jQuery each() Method You can simply use the jQuery each() method to loop through elements with the same class and perform some action based on the specific condition.


2 Answers

Use $(this)

$('.foobar').each(function(){
  $(this).blah//refers to jquery object.
});
like image 100
Stefan Kendall Avatar answered Sep 22 '22 02:09

Stefan Kendall


Within jQuery().each() you can use $(this) to use the jQuery functions on the current DOM object.

like image 45
Ty W Avatar answered Sep 21 '22 02:09

Ty W