Reputation: 37058
var example = $("#myElement")[0];
versus
var example = $("#myElement");
What's the difference? What's going on here? What does the example variable contain after each selection?
Upvotes: 1
Views: 122
Reputation: 1328
$("#myElement")[0]
selects the first item returned by your selector
$("#myElement")
gives you every item returned by the selector.
You are using an id in your example, so you should only ever have 1 item with any given id
However if you did this for example
$('.test').hide()
, it would hide every element with a class of test.
Whereas $('.test')[0].hide()
would only hide the first item
Upvotes: 0
Reputation: 227240
jQuery objects (the value returned from the $
function) are a collection ("array-like" object) of DOM elements. $("#myElement")[0]
gets the 1st element from that array, a native DOM element.
You can also do $("#myElement").get(0)
.
Upvotes: 5