www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to detect which submit button was clicked?

  1. #1
    Join Date
    Mar 2003
    Posts
    14

    How to detect which submit button was clicked?

    Can javascript determine which submit button was clicked to submit a form? My form has several submit buttons but I want to perform a particular action when a certain one is clicked. I have a validation function that gets executed when the form is submitted. What statement(s) can I use in the validation function to determine which submit button was clicked? Perhaps there is an event status that can be examined to see which button was clicked last?

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582

    Thumbs up

    Quote Originally Posted by km4hr
    Can javascript determine which submit button was clicked to submit a form? My form has several submit buttons but I want to perform a particular action when a certain one is clicked. I have a validation function that gets executed when the form is submitted. What statement(s) can I use in the validation function to determine which submit button was clicked? Perhaps there is an event status that can be examined to see which button was clicked last?
    Give each submit button a unique name/id, then use a switch/case to go that route.
    Code:
    function checkData(tid) {
       switch(tid) {
          case "submit_a":
             -- do this --;
             break;
          case "submit_b":
             -- do something else --;
             break;
          case "submit_c":
             -- do yet something else --;
             break;
          }
       }
    Code:
    <form name="testform" id="testform">
       <input type="submit" name="submit_a" id="submit_a" value="Submit A" onClick="return checkData(this.id);">
       <input type="submit" name="submit_b" id="submit_b" value="Submit B" onClick="return checkData(this.id);">
       <input type="submit" name="submit_c" id="submit_c" value="Submit C" onClick="return checkData(this.id);">
    </form>

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