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
asked Jan 20, 2011 at 14:10
Nasir
4,90511 gold badges37 silver badges39 bronze badges
-
The reason why I want the variables declared outside is that there are five of these functions...so the are declared only onceNasir– Nasir2011年01月20日 14:12:49 +00:00Commented Jan 20, 2011 at 14:12
1 Answer 1
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);
answered Jan 20, 2011 at 14:17
hellatan
3,5873 gold badges31 silver badges38 bronze badges
Sign up to request clarification or add additional context in comments.
lang-js