SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Jake1's Avatar
    Join Date
    Oct 2003
    Location
    USA
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing which button was pressed

    Hi, does anyone know how I can test, in javascript, which of several buttons was pressed when submitting a form? Currently, I use the onSubmit event handler to call a form validator function, and it is in that function which I would like to test which submit button was pressed. Anyone know?

    Thanks for any and all help!

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Several ways to do this; might as well create a custom Form.property:
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    function validate(oForm)
    {
    	alert(oForm.submitter);
    	return false;
    }
    
    </script>
    </head>
    <body>
    <form onsubmit="return validate(this)">
    <input type="text" />
    <input type="submit" name="add_button" value="Add" onclick="this.form.submitter=this.name">
    <input type="submit" name="delete_button" value="Delete" onclick="this.form.submitter=this.name">
    <input type="submit" name="destroy_button" value="Destroy" onclick="this.form.submitter=this.name">
    </form>
    </body>
    </html>
    
    Might also need some more to handle <enter> key submissions.
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast Jake1's Avatar
    Join Date
    Oct 2003
    Location
    USA
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll try it out

  4. #4
    SitePoint Enthusiast Jake1's Avatar
    Join Date
    Oct 2003
    Location
    USA
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yayyy

    It works, thanks a lot

    Oh and luckily using tab > enter to submit the form still works with that method!


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
  •