[thelist] JS: test which submit button was clicked.

Scott Brady evolt at scottbrady.net
Fri Jun 4 11:52:42 CDT 2004


Original Message:
> From: "Tab Alleman" 

> What I want to do is write a JavaScript function (TestRejection) on the
> page that contains the form that does something depending on which
> submit button the user clicks.

One option is to have a global JS variable in your page that is initially set to an empty string.

For each submit button, have an onclick handler that would set that variable to be the "value" of the clicked button.  Then, in your onsubmit script, you can test to see what the value of the variable is.

Sample code (off the top of my head, so no guarantees regarding typos and such):

<script type="text/javascript">
     var clickedBtn = '';

function setBtn(val)
{
     clickedBtn = val;
     return true;
}
</script>

<input type="submit" name="WhatToDo" value="Approve" onclick="return setBtn('Approve');" />
<input type="submit" name="WhatToDo" value="Reject" onclick="return setBtn('Reject');" />

----------------------------
Scott Brady
http://www.scottbrady.net/




More information about the thelist mailing list