Reputation: 1
Can someone explain what the [0]
represents in the following code?
$('#cars')[0].selectedIndex=0;
Thanks in advance.
Upvotes: 0
Views: 50
Reputation: 1855
jQuery Selector returns an array of elements/nodes every time.
So $(".example")
will return an array containing elements matching that selector.
In your case there may be only one element having that class name but the jQuery operates on array of elements under the hood so it will return an array containing an element on index 0
.
So in javascript you can access an element from an array using index. (in your case the 0 index)
Upvotes: 2
Reputation: 397
You can use the $ function to select multiple elements. For example, $(".example")
will select all of the elements that have the example
class.
Using [0] will return the first element as a DOM node (not as a jQuery object).
Upvotes: 1