473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

2 button form + which button was pressed

I need a form with two buttons and ability to detect which button was
pressed. I got this code to work using a javascript global variable,
but I know there must be a better way using DOM. Any suggestions?
<html>
<head>
<script language="JavaScript" type="text/javascript">
// global var: which button I am pressing
var btnWhichButton;
//================================================== ======
// Check the input fields of the form
//================================================== ======
function chkMyForm() {
alert(btnWhichButton.value);
document.SubmitExpenseForm1.action="#";

// If Delete Record
if (btnWhichButton.value == "Delete Record")
{
document.SubmitExpenseForm1.DeleteRecordParm.value = 'Y' ;
window.document.SubmitExpenseForm1.submit();
return;
}

// Otherwise do validation
chkTextArea();
document.SubmitExpenseForm1.DeleteRecordParm.value = 'N' ;
window.document.SubmitExpenseForm1.submit();
}

//================================================== ======
// Check Text Area
//================================================== ======

function chkTextArea() {
// additional validation...
alert("Extra Validation done here")
}

</script>

</head>
<body>

<form class="noLF" name="SubmitExpenseForm1"
action="javascript:chkMyForm();" >
<input TYPE=HIDDEN NAME="ExpenseCode" value="AUTO">
<input TYPE=HIDDEN NAME="AcctPeriod" value="02">
<input TYPE=HIDDEN NAME="AcctYear" value = "2006">
<input type="hidden" name="DeleteRecordParm">

<!-- Submit Record Button -->
<input type="submit" NAME="Submit" value="Submit Record"
onclick="btnWhichButton=this">

<!-- Delete Record Button -->
<input type="submit" NAME="DeleteRecord" value="Delete Record"
onclick="btnWhichButton=this">

</form>

</BODY>
</HTML>

Apr 4 '06 #1
1 2226
Bi*************@yahoo.com said on 04/04/2006 1:23 PM AEST:
I need a form with two buttons and ability to detect which button was
pressed. I got this code to work using a javascript global variable,
but I know there must be a better way using DOM. Any suggestions?
Change your function to be a property of an object and have the button's
click event set a property of that object rather than of the window
(global) object.
<html>
<head>
<script language="JavaScript" type="text/javascript">
The language attribute is deprecated, remove it but keep type.

// global var: which button I am pressing
var btnWhichButton;
//================================================== ======
// Check the input fields of the form
//================================================== ======
function chkMyForm() {
Change to:
var chkMyForm = (function()
{
var btnWhichButton;

return {
check : function(){
alert(btnWhichButton.value);

// Do real checking based on value of btnWhichButton
// Essentially put body of your current function here

},
setWhichButton : function(btn){
btnWhichButton = btn;
}
};
})();
Then in the form:

<form action="" onsubmit="chkMyForm.check()" ... >

<input type="submit" name="SubmitRecord" value="Submit Record"
onclick="chkMyForm.setWhichButton(this)">

<input type="submit" name="DeleteRecord" value="Delete Record"
onclick="chkMyForm.setWhichButton(this)">

</form>

[...]
<form class="noLF" name="SubmitExpenseForm1"
action="javascript:chkMyForm();" >
Don't munge the action attribute, use an onsubmit attribute (see above).
[...]
<!-- Submit Record Button -->
<input type="submit" NAME="Submit" value="Submit Record"
onclick="btnWhichButton=this">


It's not good to use 'Submit' as a form control name, it can too easily
become 'submit' and mask the form's submit() method. It's not a problem
here, but good to avoid anyway.
[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 4 '06 #2

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

Similar topics

2
by: jb | last post by:
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...
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...
4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
2
by: Vern | last post by:
I have a custom control which has it's own OnValidating event handler. I would like to skip the validations if the Cancel/Exit button is pressed. Also, if possible, would like to skip 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...
2
by: Krista Lemieux | last post by:
Hello, In my form I have a button, and the button_Click function (which gets called when the button is clicked). However the functionality that I have in the button_Click function, needs to be...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
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"...
1
by: abcdriver | last post by:
Later edit: Too complex: The only thing I should know is: The submit button's NAME (and value) will only be sent if that particular button was used to submit the form, so you should be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.