views:

22

answers:

3

Is this a ligitimate way of determining the selected value of an section/option box with and id of "shirt_size_1221"?

var user_id = '1221';
x = "#shirt_size_" + user_id ;                          
alert($(x + " option:selected"));
A: 

You can get the value of a selected option in a dropdown box by using

var selectList = document.getElementById("shirt_size_1221");
var val = selectList.options[selectList.selectedIndex].text;

No JQuery needed.

jduren
A: 

Your alert statement will always show [object] unless you change it to

alert($(x+' option:selected').val());

And don't worry, this is a good way of getting selected option.

Danny Chen
A: 

The "proper" way is simply:

var user_id = '1221';
x = "#shirt_size_" + user_id ;        
alert($(x).val());
sje397
Docs [here](http://api.jquery.com/val/)
sje397