Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer How IE handles getAttribute('id') when attribute is missing

    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/att...ightmare-in-ie)
    Last edited by windy; 02-10-2007 at 04:50 AM.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by windy View Post
    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/att...ightmare-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:
    Code:
    if( typeof elem.id != "string" || elem.id=="" )
     { /* Doesn't exist or not specified */  }
    Should do it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •