I’ve created an array of elements called $images (all the elements in the hidden class.) Then when I try to apply any method to just one element in the array, I get a $images[1].attr is not a function error. However, when I try $images.attr('id') for example without specifying the index of the array, it works but gives me the result for the first element in the array only.
$images = $(".hidden");
alert($images[1].attr('id'));
What’s going here and how can I apply methods to single elements in an array? By the way, I’m certain there are at least two elements in the array as I tested it for this.
If you want still to have a jQuery object, rather than retrieving a native DOM element object, you need to use the
eqfunction. This gets an element at a position in the array and returns it wrapped in the jQuery object, so you can do jQuery operations on it.So:
If you only want the DOM element, you can use the square bracket notation or the
getmethod. You can then look up a DOM property directly: