0

For some reason, jQuery isn't giving me the attributes of objects in array when I define the number in the array. It will return the class of the first object in an array when none in particular are defined, though. For example, this works:

$('#content').prepend($('div #left ol.group li',data).attr("class"));

but this doesn't:

$('#content').prepend($('div #left ol.group li',data)[3].attr("class"));

Can someone explain to me why this doesn't work like most other functions do and how to make it work?

0

2 Answers 2

4

with jQuery get the index by using eq

$('#content').prepend($('div #left ol.group li',data).eq(3).attr("class"));
Sign up to request clarification or add additional context in comments.

1 Comment

This would be a +1 if you told him why as well as what. :-) (E.g., because when you index into the jQuery object, what you get back is a raw DOM element, not a jQuery object.)
3

Accessing an individual element of a jQuery set using the indexer returns a DOM object, which doesn't have the .attr() method. You want .eq(3), which returns a jQuery object wrapping that individual element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.