SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getAttribute() won't work in IE

    I am trying to use getAttribute() to get the "for" attribute of a label, but it won't work in IE. Originally, I had used htmlFor and it worked fine in both IE and Firefox. Does anyone know why getAttribute() won't work in IE. W3C has getAttribute as being available in IE as early as IE5.

    This is the code snippet inside my onclick event handler:

    Code:
    this.parentNode.getAttribute('for')

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    for

    I don't think you can use "for" for an attribute name. Javascript will interpret this as the beginning of a for loop eg. for(var i = 0; ..... and will error. If this is a "custom" attribute, I would go back to using htmlFor or something else that won't mean anything special to javascript.

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE is stupid. This probably would work in IE:
    Code:
    this.parentNode.getAttribute('htmlFor')
    I don't use getAttribute() or setAttribute() because of IE's stupidity and because using the old DOM0 way works and requires a little less code. So, I recommend you just use this:
    Code:
    this.parentNode.htmlFor
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

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