Susan
Susan

Reputation: 1832

jQuery: pass parameter to function

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()));
            })
        }

Upvotes: 0

Views: 689

Answers (2)

Matt S
Matt S

Reputation: 1882

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 **]);
}

Upvotes: 0

BNL
BNL

Reputation: 7133

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()));
        })
    }

Upvotes: 1

Related Questions