I have some jQuery code that copies the selected values from one listbox to another:
$(adPerson + " option:selected").each(function(){
$(toNames).append($(this).clone());
});
I’m trying to make sure no blank lines are added to the destination listbox, so I tried this:
$(adPerson + " option:selected").each(function(){
if($(this).text != "")
{
$(toNames).append($(this).clone());
}
});
The above code does not work – I think is is because of the way .each is used.
Any ideas?
Derek
First off, you need to use
text()instead oftextif you wanted to test that way. It is a function, not a property.However, all you need to is use the
:not(:empty)jQuery selector:You could further simplify it by using this: