PositiveGuy
PositiveGuy

Reputation: 47743

Undefined options for dropdown

I am getting an error that options is undefined. The dropdown shows up but for some reason options is not accessible

dropdown.options is undefined [Break On This Error] dropdown.options.length = ObjectCount(equtypeList);

$(dropdown).show(); // this works

if (ObjectCount(equipmentTypeList) > 1) 
{
    $(dropdown).attr("disabled", false); //this works
    dropdown.options.length = ObjectCount(equipmentTypeList);  //bombs out here saying options is null...wtf??
    AddDropdownOption("-- Select Equipment Type--", "-1", dropdown.id);
}

So I don't get it. If I reshow a dropdow, then why is the option attribute not accessible (not usable cause it's null). Clearly it's there as I can see the dropdown reappear on the page...but has no option attribute? That just doesn't seem right that I would get null for the options attribute. If it's enabled and reappears, then it's a valid dropdown and I should be able to add stuff to it agani.

Upvotes: 0

Views: 1668

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262979

That's because dropdown is a jQuery object, obtained through a call to $(). jQuery objects do not expose an options property, the underlying DOM elements do.

You can use indexing syntax or the get() method to obtain the DOM element:

dropdown[0].options.length = ObjectCount(equipmentTypeList);
// or
dropdown.get(0).options.length = ObjectCount(equipmentTypeList);

You could also build another jQuery object containing the options:

$("option", dropdown).length = ObjectCount(equipmentTypeList);
// or
dropdown.find("option").length = ObjectCount(equipmentTypeList);

However, assigning to the length property of a jQuery object doesn't have any effect on the DOM, so this approach probably won't give the results you're expecting.

Upvotes: 1

Related Questions