document.getElementById(el) has no properties.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    document.getElementById(el) has no properties.

    I'm trying to implement an onfocus and onblur background-color changing. But, as per usual, it ain't jivin'.

    Here's the function

    Code:
    function bg(el, col)
    {
      
      document.getElementById(el).style.backgroundColor = col;
      
    }
    and here's how it's being called
    Code:
    onblur="bg(this, '#000')"
    Any idea why it's saying document.getEle mentById(el) has no properties?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    document.getEle mentById() expects an ID string, so either use el.style... or pass the id to the function.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Cheers for that.

      I did as you suggested and changed it to el.style..

      :)

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        When you pass this as an argument to a function, it refers to the owner of the function. So in case a function is contained in an HTML tag, the owner is that HTML tag (DOM element to be exact).
        Code:
        <[u]input[/u] onblur="bg(this, '#000')" . . . . >
           ^                |
           '----------------'
        So you should have either used this.id as argument, or the better is to do el.style, as acoder said.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by hsriat
          When you pass this as an argument to a function, it refers to the owner of the function. So in case a function is contained in an HTML tag, the owner is that HTML tag (DOM element to be exact).
          Code:
          <[u]input[/u] onblur="bg(this, '#000')" . . . . >
             ^                |
             '----------------'
          So you should have either used this.id as argument, or the better is to do el.style, as acoder said.
          Thanks for that elaboration there, hs. ;)

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Not a problem :)
            I'm always ready for explaining something which I know, and as much as I know.
            That's a different thing that according to this, I'm left with few things to explain.

            Comment

            Working...