473,597 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/javascript">
// global var: which button I am pressing
var btnWhichButton;
//=============== =============== =============== ===========
// Check the input fields of the form
//=============== =============== =============== ===========
function chkMyForm() {
alert(btnWhichB utton.value);
document.Submit ExpenseForm1.ac tion="#";

// If Delete Record
if (btnWhichButton .value == "Delete Record")
{
document.Submit ExpenseForm1.De leteRecordParm. value = 'Y' ;
window.document .SubmitExpenseF orm1.submit();
return;
}

// Otherwise do validation
chkTextArea();
document.Submit ExpenseForm1.De leteRecordParm. value = 'N' ;
window.document .SubmitExpenseF orm1.submit();
}

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

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

</script>

</head>
<body>

<form class="noLF" name="SubmitExp enseForm1"
action="javascr ipt:chkMyForm() ;" >
<input TYPE=HIDDEN NAME="ExpenseCo de" value="AUTO">
<input TYPE=HIDDEN NAME="AcctPerio d" value="02">
<input TYPE=HIDDEN NAME="AcctYear" value = "2006">
<input type="hidden" name="DeleteRec ordParm">

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

<!-- Delete Record Button -->
<input type="submit" NAME="DeleteRec ord" value="Delete Record"
onclick="btnWhi chButton=this">

</form>

</BODY>
</HTML>

Apr 4 '06 #1
1 2240
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="JavaS cript" 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(btnWhichB utton.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="chkMy Form.check()" ... >

<input type="submit" name="SubmitRec ord" value="Submit Record"
onclick="chkMyF orm.setWhichBut ton(this)">

<input type="submit" name="DeleteRec ord" value="Delete Record"
onclick="chkMyF orm.setWhichBut ton(this)">

</form>

[...]
<form class="noLF" name="SubmitExp enseForm1"
action="javascr ipt: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="btnWhi chButton=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.c om/FAQ>
Apr 4 '06 #2

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

Similar topics

2
41043
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 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" >
8
14636
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 the form. If submit button2 is pressed I need to make sure that only textfied1 is filled before submitting the form.
4
8933
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 have their own set of fields they can change. Therefore I use setfocus property in the lostfocus event for some of the users to get from one control to another. Since I am "directing traffic" with setfocus, the users are unable to use the...
2
2531
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 validations if the X was clicked to close the form. How can I do this? Thanks, Vern
7
1830
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 values to be added to a database and sent by email. 1. I have the code which submits the form values to the database. (See at
2
2792
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 in the Page_Load, since I need to change a value based on the button click to render the page. So my question is, can I access the button object (the sender of the event) in the Page_Load. So that in the Page_Load I can have the following...
19
2172
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 enter.
10
5517
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 pressed, submit the form - bypassing my onclick handler. I can partially fix this with: <form onsubmit='return false;'>
3
2496
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" id="btnCancel"> </form>
1
6245
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 check that in the server side script. Rest is history:
0
7969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.