473,473 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
2 41018
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
htolly
1 New Member
Thank you for this coding - i have spent hours trying to achieve this
Jul 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Syed Ali | last post by:
Hello, I have 1 HTML form with 4 submit buttons and 10 textfield entry areas. If submit button1 is pressed I need to make sure that all 10 textfield entries have been filled before submitting...
15
by: Mattia | last post by:
Hi! I have a <form> that can be submitted thruogh three buttons and I need to tell witch one was pressed when the form was submitted. I now do it with 3 <input type="submit" name="..."...
4
by: karenmiddleol | last post by:
I have the following form the user enters the From and to period and presses the Submit button and the form fields are cleared once the submit button is pressed. Is there a way I can keep the...
7
by: Miguel Dias Moura | last post by:
Hello, I am working in ASP.net / VB with Access Database. i have a Multipage in my web site with the Submit button as the last button. When the Submit button is pressed i want the form...
3
by: Xarky | last post by:
Hi, I have a page with 2 textboxes(Name and Surname) and two buttons(Submit and Reset). Now I have set the tab for them 1 Name 2 Surname 3 Submit 4 Reset
3
by: Robert W. | last post by:
I'm new to ASP.net programming so excuse my ignorance if the following question seems overly simplistic. I've created a simple Login form with 3 primary WebControls: - A TextBox for the Username...
10
by: Tim Streater | last post by:
I have a form and a button to submit it. The button is made from: <input type=button onclick='myHandler(this.form);'> This all works fine except that in Safari 2.0.4, the enter/return keys, if...
3
by: Daniel | last post by:
Hi all, I am using .NET 1.1. I have a form with a cancel button, something like this: <form method="post"....> <input type="submit" value="Submit"> <input type="submit" value="Cancel"...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.