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

J

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....
 
S

Stephen

Hi ...
jb said:
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;
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top