Tuesday, 10 September 2013

Adding option in select based on current options

Adding option in select based on current options

I have a function that loops through data and dynamically creates options
inside a select. The problem is I only want one "please select" option to
show up, not one for each time the loop runs. So basically, if this option
already exists don't add it again. I tried this code but it will not work,
it doesn't show this option at all.
if ($("#level"+num+" option[value = 'base']").length < 0) {
$("#level"+num ).prepend($("<option></option>")
.attr("value", "base")//.id )
.text( "Please select an option" )
);
};

No comments:

Post a Comment