www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] get id of <input type='button'> onclick

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Posts
    64

    resolved [RESOLVED] get id of <input type='button'> onclick

    Hello,

    I have multiple buttons as follows:

    <input type='button' id='Q01Clear' name='Q01Clear' value='Clear' onclick='ClearQ();' />

    The id's increment (ie: Q01Clear, Q02Clear, etc. etc...), and the onclick calls the same function.

    I now have this:

    function ClearQ()
    {
    var docButtons=document.getElementsByTagName('input')
    for (var i=0; i<docButtons.length; i++){ if (docButtons[i].type == 'button'){ alert(docButtons[i].id) }
    }

    Ok, so this will alert to me the id's of all the "<input>" tags of type 'button'. How then, can I alert only the id of the button that is clicked?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    <input type='button' id='Q01Clear' name='Q01Clear' value='Clear' onclick='alert(this.id);' />
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Sep 2011
    Posts
    64
    Thanks Padonak!

    Ok, but when I tried to do the same thing by calling the funciton from an external js file like so:

    onclick='ClearQ();' />

    function ClearQ() { alert (this.id) }

    ... it returns 'undefined' as the id ???

    Is there a way to get funciton ClearQ() {}, to alert the id of the button that is clicked?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Code:
    // With this HTML line (this is the DOM input object and this.id is id)
    <input type='button' id='Q01Clear' name='Q01Clear' value='Clear' onclick='ClearQ(this.id);' />
    
    // use this function (clikedId is the argument of the function it takes different values according to the different calls)
    function ClearQ(clikedId){
       alert(clikedId);
    }
    
    // Or with this line (event is an objet which is create with the click)
    <input type='button' id='Q01Clear' name='Q01Clear' value='Clear' onclick='ClearQ(event);' />
    
    // use this function to display the target of the click
    function ClearQ(e){
       //e is the event which can take different forms with the browser
      // we take e.target if is form exits otherwise we take e.srcElement
       var target=e.target?e.target:e.srcElement;
       alert(target.id);
       // or alert(target.name);alert(target.value);alert(target.type)
    }
    Last edited by 007Julien; 04-21-2012 at 05:09 PM.

  5. #5
    Join Date
    Sep 2011
    Posts
    64
    Thanks, 007Julien!

    My stuff is working.
    Oh yeah ...

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Also you can show these id's right in the page instead of using alerts - this is much handy because every alert stops the script untill you click OK. For this purpose you must add the appropriate element in the page markup, for example:

    HTML Code:
    <div id="my_results"></div>
    JS
    Code:
    function ClearQ(e){
       //e is the event which can take different forms with the browser
      // we take e.target if is form exits otherwise we take e.srcElement
       var target=e.target?e.target:e.srcElement;
       document.getElementById('my_results').innerHTML=target.id;
       // or alert(target.name);alert(target.value);alert(target.type)
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles