SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Identifying what button was clicked.

    If you have a collection of submit buttons on one page, and an OnSubmit event fired on the Form, how can you find out the ID of the button clicked, triggering the form submit?

    Code:
    <script>
    
    function doSomething(){
    
     //Find out which button was clicked???
    
    }
    
    </script>
    
    <form onSubmit="doSomething()" action="">
    
    <input type="submit" name="button_1" value="Button 1" id="button_1"  />
    <input type="submit" name="button_2" value="Button 2" id="button_2"  />
    <input type="submit" name="button_3" value="Button 3" id="button_3"  />
    <input type="submit" name="button_4" value="Button 4" id="button_4"  />
    <input type="submit" name="button_5" value="Button 5" id="button_5"  />
    
    </form>

  2. #2
    SitePoint Zealot coothead's Avatar
    Join Date
    Jan 2005
    Location
    Chertsey, London, England
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there findel,

    does this help...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    <!--
    
      var but_id;
    
    function doSomething(){
       alert(but_id); //find out which button was clicked???
     }
    
    window.onload=function() {
      inp=document.forms[0].getElementsByTagName('input');
    for(c=0;c<inp.length;c++) {
    if(inp[c].type=='submit') {
       inp[c].onclick=function() {
       but_id=this.id;
        }
       }
      }
     }
    //-->
    </script>
    
    </head>
    <body>
    
    <form onsubmit="return doSomething()" action="#">
    <div>
    <input type="submit" name="button_1" value="Button 1" id="button_1"  />
    <input type="submit" name="button_2" value="Button 2" id="button_2"  />
    <input type="submit" name="button_3" value="Button 3" id="button_3"  />
    <input type="submit" name="button_4" value="Button 4" id="button_4"  />
    <input type="submit" name="button_5" value="Button 5" id="button_5"  />
    </div>
    </form>
    
    </body>
    </html>
    
    
    
    
    coothead

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Coothead, i've been away and just come back to this. Thanks for that suggestion - it looks like a good idea. But, I'm supprised if there isn't a more built in way of doing this in javascript.

    Thank you!


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
  •