tags:

views:

43

answers:

4

I have a listbox which (sometimes) uses OptionGroups as well, meaning that if I select the parent of a certain OPTION in my listbox, as follows:

var optionGroup = $(this.options(index)).parent();

it can either be an OptionGroup (if they're used), or the Select element itself.

Is there an easy way of determining which kind of object I'm dealing with here?

+1  A: 

The information you are looking for is the tagName property of the DOM element. You'll get hold of this from a jQuery element like so:

$("#element")[0].tagName

before comparing it, It's advisable to do a .toLowerCase() on the property.

Pekka
+1  A: 

I think it could work with

if($(this.options(index)).parent().is('select'))
{
    //your code here
}
JDT
+1  A: 

You could use .parent('optgroup'). This will filter out anything that isn't an optiongroup. Then you'd have to check if you get a result of course.

Peter
+1  A: 

Just throwing this out there, if you're actually wanting this to determine if you need another .parent() to get the <select>, there's a method for this, .closest(), like this:

$(this.options(index)).closest('select');
Nick Craver