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 4 of 4
  1. #1
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts

    getAttribute help

    I have this function that I'm using on elements to add there name into a form element, but I don't want to write it out on every element but when I use it in a function it throws an error saying that "this.getAttribute()" is not a function.
    Here is my function.
    Code:
    function updateForm(el_id){
         document.getElementById(el_id).value = this.getAttribute('name');
    }
    Last edited by ninnypants; 07-31-2008 at 03:23 AM.

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    thats because we don't access attributes through a method such as getAttribute().

    we access the attributes through the method '.attribute'
    if you want to get the name of an element:

    Code:
    this.name
    
    ...or...
    
    document.getElementById("an_id").name
    with that in mind your code becomes:

    Code:
    function updateForm(el_id,curr_id)
    {
         document.getElementById(el_id).value = curr_id.name;
    }
    and you would call the function thus:

    Code:
    onClick="UpdateForm(an_id, this);"
    
    or onblur, onfocus, on mouseover, mouseout, etc etc..

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Mike, I have to disagree.

    We can access the element's name via getAttribute().

    The problem at ninnypant's end is that he uses the this keyword which was referencing to the updateForm function and not the element where the onclick was called (I suppose).

    The fix is by adding an additional parameter on updateForm function. Highlighted should be added and the one in red should be replaced with e:
    Code:
    function updateForm(el_id,e){
         document.getElementById(el_id).value = this.getAttribute('name');
    }
    Together with this ammendment is to send the this keyword to our function. Add highlighted on each function call:
    Code:
    updateForm('elementID','this')
    Hope it makes sense.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    ninnypants (07-31-2008)

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Quote Originally Posted by rangana View Post
    Mike, I have to disagree.

    We can access the element's name via getAttribute().

    The problem at ninnypant's end is that he uses the this keyword which was referencing to the updateForm function and not the element where the onclick was called (I suppose).

    The fix is by adding an additional parameter on updateForm function. Highlighted should be added and the one in red should be replaced with e:
    Code:
    function updateForm(el_id,e){
         document.getElementById(el_id).value = this.getAttribute('name');
    }
    Together with this ammendment is to send the this keyword to our function. Add highlighted on each function call:
    Code:
    updateForm('elementID','this')
    Hope it makes sense.
    It didn't work at first but now it's working thanks.


  •  

    Posting Permissions

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