I want to pass a parameter into my function. The parameter will be a variable that representsa CSS class name. Based upon which checkbox was selected I will set the value of the variable (eg, if CheckBox1 was selected the value of the variable would be the class name '.list1').
The dropdownlist has a large number of options and I want to select a subset of the options based on their 'class' and use those options to populate another list (fl_listEmpty).
Here is my code as far as I've gotten:
function FillDropDownList(parameter) {
$('.fl_list1 [**put class name parameter here**]).each(function (index) {
$(".fl_listEmpty").append($('<option> </option>').val(index).html($(this).text()));
})
}
-
1possible duplicate of jQuery : put a variable AND a selector inside the $('selector')? ... please use the search before you ask a question. This was asked like ... a lot of timesFelix Kling– Felix Kling2011年09月22日 19:52:54 +00:00Commented Sep 22, 2011 at 19:52
2 Answers 2
Just compose the selector as if it were a string (which it is).
function FillDropDownList(parameter) {
$('.fl_list1 ' + parameter).each(function (index) {
$(".fl_listEmpty").append($('<option> </option>').val(index).html($(this).text()));
})
}
Comments
Assuming your space is intentional between classes, a find would be appropriate. If it is possible that the value may be undefined, you could just make a condition:
function FillDropDownList(parameter) {
var selset = parameter ? $('.fl_list1').find('.' + parameter) : $('.fl_list1');
selset.each([** your function here **]);
}