Reputation: 952
I wanted to get the following code dynamic:
var items = $("#scenario_select option");
Above should become (with dropDownObj= $("#scenario_select");
:
function someFunction(dropDownObj){
var items = $(dropDownObj + " option");
var items = $("#" + dropDownObj + " option");
}
This both doesn't work. I did not find any help on that (only without the options tag)
Any help is greatly appreciated.
Upvotes: 0
Views: 78
Reputation: 1665
Try this:
function someFunction(dropDownObj){
var items = $('#' + dropDownObj.prop('id') + " option");
}
But I suppose a more 'proper' way to do it would be to use find as suggested by @Exception, or use:
function someFunction(dropDownObj){
var items = $("option", dropDownObj);
}
Which basically does the same thing as .find(). See This SO question for a comparison between the two (tl;dr: They are pretty much equivalent).
To find the number of items (as you noted in the comments) you could also use:
function someFunction(dropDownObj){
var items = $("option", dropDownObj).length;
}
Which is a bit shorter than the solution you posted in the comments.
You should also note that since
dropDownObj= $("#scenario_select");
It is already a jQuery object, and you don't need to wrap it using $. With this in mind you solution becomes:
var items = dropDownObj.children('option').length;
Upvotes: 6
Reputation: 8379
This should work.
var dropDownObj = $('#yourDropDownID');
dropDownObj.find('option')
Upvotes: 3