I have a div with an id="my_div". How can I find a <select> by name (i.e. name="my_select") assuming that the <select> can be at any level underneath the div? I.e. it can be a child, grandchild etc. Ultimately I need to get the value of the selected option.
views:
27answers:
2
+3
A:
This will search through all descendants of #my_div for <select> elements where the name attribute equals my_select.
var $select = $('#my_div select[name="my_select"]')
To get the value of the currently selected option, use .val().
var value = $select.val();
patrick dw
2010-09-28 18:56:40
thanks! how can I grab the value of the selected option at this point?
bba
2010-09-28 18:59:01
@bba - Just use `.val()`. I'll update. :o)
patrick dw
2010-09-28 19:00:38
+1
A:
This should work
var mySelect = $('#my_div select[name="my_select"]')[0]
This will get you the selected option. Meaning the actual DOM Element
alert(mySelect.options[mySelect.selectedIndex].value);
alert(mySelect.options[mySelect.selectedIndex].text)
John Hartsock
2010-09-28 18:57:39