How to select second div using jQuery in a webpage?
You don't need jQuery:
var secondDiv = document.getElementsByTagName('div')[1];
getElementsByTagName('div')
gets an array of all div
s on the page, then you get the second element out of that (zero-indexed) array with [1]
.
You could also apply a jQuery wrapper, if you need jQuery functionality:
var $secondDiv = $(document.getElementsByTagName('div')[1]); //($ in the var name is merely used to indicate the var contains a jQuery object)
Example
The following will get the second div using the eq
method:
$("div:eq(1)");
EXAMPLE
Please note that @Cerbrus's answer is also correct, you can do this without jQuery.
You could also use the nth-child
selector.
http://api.jquery.com/nth-child-selector/
Stangely the :nth-child
did not work for me. I ended up using
$(someElement).find("select").eq(0)