I have some javascript where I append an option to a select:
var myuniqueddl = $('#myuniqueddl');
$("<option value='0'>--- Select a value ---</option>").appendTo(myuniqueddl);
I’m actually trying to do this for another select as well.
I’m wondering to avoid code duplication should I be passing the ddl to a method to do this?
function(someType ddl)
{
$("<option value='0'>--- Select a value ---</option>").appendTo(ddl);
}
Is this a bad idea to be passing a select to a method?
Any better way of doing this?
If this way is ok what type do I pass it in as?
Firstly, yes, it’s completely valid to pass a
selectelement into a function to reduce code duplication. In this case, it seems like you’re worried about having to type the same function call (appendTo) twice, which really isn’t duplication.Secondly, you cannot specify any type for a JavaScript function parameter. Variables in JavaScript are dynamically typed. You simply specify the variable name: