Asur
Asur

Reputation: 2057

Working with Array like JQuery objects's methods

I am new to jQuery. I am having trouble working with array like jQuery objects's methods (I don't know if its right name) like .height() and .animate() which affect only the first element of what the query selector matches.

I do not know how to make them work with desired single element.

Like the 3rd element of what $('li') returns or loop into results of $(li) and get height of each.

Update:

Learnt that $(this)[0] === this

Upvotes: 0

Views: 35

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Like the 3rd element of what $('li') returns

For this, use eq():

var $li = $('li').eq(3); // $li now contains the third li element

loop into results of $(li) and get height of each.

For this, use each():

$('li').each(function() {
    console.log($(this).height());
});

Note that within the each function block, this refers to the DOM element of the current iteration.

Upvotes: 2

tymeJV
tymeJV

Reputation: 104775

You can iterate through sets of elements via .each like so:

$(".someClass").each(function() {
    var h = $(this).height(); //height of current iterated elem
});

Or use :eq in a selector to get an element from a desired position:

$(".someClass:eq(3)").height(); //get elem at position 3

Upvotes: 0

Related Questions