0

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

1
  • The reason why I want the variables declared outside is that there are five of these functions...so the are declared only once Commented Jan 20, 2011 at 14:12

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I might be wrong, but select and option are not used in the function
ah, no, you're right @pimvdb. select is only referenced outside, and i have a typo with option - should be options, good catch =)
def np, nasir. glad i could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.