Omar Ismail Kamel
Omar Ismail Kamel

Reputation: 1

I don't understand the "0" in [0].selectedIndex

Can someone explain what the [0] represents in the following code?

$('#cars')[0].selectedIndex=0;

Thanks in advance.

Upvotes: 0

Views: 50

Answers (2)

anees
anees

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

Zoldszemesostoros
Zoldszemesostoros

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

Related Questions