...

View Full Version : How IE handles getAttribute('id') when attribute is missing



windy
02-09-2007, 10:48 PM
I am traversing a list of element nodes where some have the id attribute and others don't. When I use getAttribute('id') in Opera 9 or FF2 and the attribute doesn't exist, these browser return null and typeof=object. In IE(7) I get an empty value with typeof=string with length=0.

So, I have to use :

attr = elm.getAttribute('id');
if(typeof(attr)=='string'){
if(attr.length>0){alert('attribute doesnt exist');}
}

Is this the appropriate cross browser work around ?

(see also http://tobielangel.com/2007/1/11/attribute-nightmare-in-ie)

Arty Effem
02-10-2007, 06:27 PM
I am traversing a list of element nodes where some have the id attribute and others don't. When I use getAttribute('id') in Opera 9 or FF2 and the attribute doesn't exist, these browser return null and typeof=object. In IE(7) I get an empty value with typeof=string with length=0.

So, I have to use :

attr = elm.getAttribute('id');
if(typeof(attr)=='string'){
if(attr.length>0){alert('attribute doesnt exist');}
}

Is this the appropriate cross browser work around ?

(see also http://tobielangel.com/2007/1/11/attribute-nightmare-in-ie)That doesn't account for it not being type string.

I would read the property directly. A specified id must be type string, so:

if( typeof elem.id != "string" || elem.id=="" )
{ /* Doesn't exist or not specified */ }
Should do it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum