467,917 Members | 1,397 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,917 developers. It's quick & easy.

How do you know what button was pressed in the submit?

jb
Hello, I need to know which button was pressed in the submit , i tried
reading the vaule of submit it the validateDate function but it returns
'undefined' value ; I do this in asp all the time, Not sure how to do it in
javascript

<form name="form1" method="post" action="myNewplace.asp"
ONSUBMIT="return ValidateData();">

<input type="Submit" name="Submit" value="Save" >

<input type="Submit" name="Submit" value="Submit" >

<input type="Submit" name="Submit" value="Cancel" >
...

function ValidateData(Button) {
var CanSubmit = false;
if (document.forms[0].Submit.value=='Cancel') {
return true;
}
...

thanks for your help....
Jul 20 '05 #1
  • viewed: 40523
Share:
2 Replies
Hi ...
jb wrote:
Hello, I need to know which button was pressed in the submit , i tried
reading the vaule of submit it the validateDate function but it returns
'undefined' value ; I do this in asp all the time, Not sure how to do it in
javascript
If I understand rightly, you want the ValidateData() function to know
which submit button you clicked. Right?

I can think of a solution using a global variable for the button clicked:

<form name="form1" method="post" action="myNewplace.asp"
ONSUBMIT="return ValidateData();">

<input type="Submit" name="Submit" value="Save" >

<input type="Submit" name="Submit" value="Submit" >

<input type="Submit" name="Submit" value="Cancel" >


I'd add an onclick handler in each of these <input>s. This onclick
handler assigns the button-object clicked to the global variable:

<input type="submit" name="Submit" value="Save"
onclick="btnWhichButton=this">
I'd use a script similar to:

<script type="text/javascript">

var btnWhichButton; // the global variable

function ValidateData() {
if (btnWhichButton.value == 'Cancel' ) {
alert('Cancel Pressed');
} else if (btnWhichButton.value == 'Save' ){
alert('Save Pressed');
} else if (btnWhichButton.value == 'Submit' ){
alert('Submit Pressed');
return true;
}
return false;
}
</script>


function ValidateData(Button) {
Since this solution uses a global var, you don't need to pass in any
parameter.
var CanSubmit = false;
if (document.forms[0].Submit.value=='Cancel') {
Also, don't refer to the button using dom reference, use the global
variable since this is a "button object"
if ( btnWhichButton.value == 'Cancel')
return true;
}
...


HTH,
Stephen

Jul 20 '05 #2
Thank you for this coding - i have spent hours trying to achieve this
Jul 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Syed Ali | last post: by
7 posts views Thread by Miguel Dias Moura | last post: by
3 posts views Thread by Xarky | last post: by
3 posts views Thread by Robert W. | last post: by
10 posts views Thread by Tim Streater | last post: by
3 posts views Thread by Daniel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.