473,661 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript event question

When there is a HTML Form with lots of input elements, how can I know
which element is triggering the form submit action with the help of
javascript.

Feb 9 '06 #1
2 1261
VK

khng wrote:
When there is a HTML Form with lots of input elements, how can I know
which element is triggering the form submit action with the help of
javascript.


Let's restore the intended message first ;-)

"I have a HTML form with several SUBMIT buttons in it. When user
submits the form I need to detect which one of submit buttons has been
used. Please help me. Thank you in advance."

You are welcome. This question is not a trivia at all, because onsubmit
the only visible source of event is the form itself. I guess the
possibility to have several submit buttons with different values was
not noticed while designing the form event model.

Good news is that "click" event fires before "submit" event. Bad news
is that you need to loop through all INPUT's to get the right ones and
attach onclick handlers to them. All together it comes to something
like:

<html>
<head>
<title>Detect submit button</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var submitter = null;

function validate(f) {
alert(submitter .value);
return false;
}

function getSubmitter(e) {
if ((e)&&(e.target )) {
submitter = e.target;
}
else if ((event)&&(even t.srcElement)) {
submitter = event.srcElemen t;
}
else {
/*NOP*/
}
}
function init() {
var ins = document.forms[0]
.getElementsByT agName('INPUT') ;
for (var i=0; i<ins.length; i++) {
if (ins[i].type.toLowerCa se() == 'submit') {
ins[i].onclick = getSubmitter;
}
}
}

window.onload = init;
</script>
</head>

<body>
<form method="get" action="" onsubmit="retur n validate(this)" >
<input type="submit" name="Submit1" value="Submit1" >
<input type="submit" name="Submit2" value="Submit2" >
</form>

</body>
</html>

Feb 9 '06 #2
VK wrote:
khng wrote:
When there is a HTML Form with lots of input elements,
how can I know which element is triggering the form
submit action with the help of javascript.
Let's restore the intended message first ;-)


Are you criticising someone else's English? That is very hypocritical of
you considering that you employ grammar so perverse that about 25% of
what your write is rendered incoherent gibberish from which nobody can
extract meaning.
"I have a HTML form with several SUBMIT buttons in it.
When user submits the form I need to detect which one of
And if you are going to correct people the least you should do
suggest corrections made of complete sentences. That should
be "When _the_ user submits ...".

<snip> ... . Bad news is that you need to loop through all
INPUT's to get the right ones and attach onclick
handlers to them. All together it comes to something
like: <snip> function getSubmitter(e) {
if ((e)&&(e.target )) {
submitter = e.target;
}
else if ((event)&&(even t.srcElement)) {
submitter = event.srcElemen t;
}
else {
/*NOP*/
}
}
function init() {
var ins = document.forms[0]
.getElementsByT agName('INPUT') ;
for (var i=0; i<ins.length; i++) {
if (ins[i].type.toLowerCa se() == 'submit') {
ins[i].onclick = getSubmitter;
}
}
}

<snip>

So after all this time, and having had it explained to you, and in
considerable detail, you still do not understand that in an event
handler attached to the property od a DOM element that corresponds with
an event handling attribute the - this - keyword reliably refers to the
DOM element itself, so there is not need to mess around with testing or
normalising event objects at all.

It seems that whatever you do you insist upon doing it in the worst
possible way.

Richard.
Feb 12 '06 #3

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

Similar topics

6
2523
by: den 2005 | last post by:
Hi everybody, Question 1: How do you set the values from server-side to a client-side control or how do you execute a javascript function without a button click event? Question 2: How do you get response from a Confirm() popup window to uncheck all server-side checkboxes placed in a panle of a web user control? I am using ASP.Net 2.0, Thanks. Need info...
0
8428
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
8851
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
8754
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
8542
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
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5650
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
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1740
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.