Wednesday, May 11, 2011

Tip: Proper use of the jQuery "get" function

jQuery provides a function which gets a DOM element based on the index passed in. It's important to write the correct syntax at the selector part for it to work properly. For example,

let say I have a variable named "items" that is a drop down list with several options as represented at the html code below:

This will not work
items.get(1) --> TypeError: items.get is not a function

This will work
$('option', items).get(1) --> return option with value 2

No comments: