Nasir
Nasir

Reputation: 4865

How do I pass arguments to my JavaScript function?

I have this JavaScript function:

function prefixOneDropDown(){

        var source = $('#prefix-one');
        var selected = source.find('option[selected]');
        var options = $('option', source);  
        var DefinitionList = '<dl id="dd-prefix-one" class="dropdown f-left"><dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt><dd><ul></ul></dd></dl>';
        $(DefinitionList).insertAfter(source);

        options.each(function(){ $("#dd-prefix-one dd ul").append('<li><a href="#"><span class="number-plate">' + $(this).text() + '</span><span class="value">' + $(this).val() + '</span></a></li>'); });
    }

I was wondering if there is a way declaring var selected, options, DefinitionList outside the function...then call them in as arguments?

I've tried a number of different approaches but I'm missing something.

The function is called in my js like so : prefixOneDropDown();

Any help would be Greatly Appreciated, Thanks

Upvotes: 0

Views: 152

Answers (1)

hellatan
hellatan

Reputation: 3577

did you try this:

function prefixOneDropDown(source, options, dl){

    $(dl).insertAfter(source);

    options.each(function(){ $("#dd-prefix-one dd ul").append('<li><a href="#"><span class="number-plate">' + $(this).text() + '</span><span class="value">' + $(this).val() + '</span></a></li>'); });
}

var source = $('#prefix-one'),
    selected = source.find('option[selected]'),
    options = $('option', source),
    DefinitionList = '<dl id="dd-prefix-one" class="dropdown f-left"><dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt><dd><ul></ul></dd></dl>';

prefixOneDropDown(source, options, DefinitionList);

Upvotes: 2

Related Questions