By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,433 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,433 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: 1
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.