473,385 Members | 1,919 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,385 software developers and data experts.

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 1234
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)&&(event.srcElement)) {
submitter = event.srcElement;
}
else {
/*NOP*/
}
}
function init() {
var ins = document.forms[0]
.getElementsByTagName('INPUT');
for (var i=0; i<ins.length; i++) {
if (ins[i].type.toLowerCase() == 'submit') {
ins[i].onclick = getSubmitter;
}
}
}

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

<body>
<form method="get" action="" onsubmit="return 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)&&(event.srcElement)) {
submitter = event.srcElement;
}
else {
/*NOP*/
}
}
function init() {
var ins = document.forms[0]
.getElementsByTagName('INPUT');
for (var i=0; i<ins.length; i++) {
if (ins[i].type.toLowerCase() == '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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.