Solved

Determining submit button pressed without onClick event handler

Posted on 2004-04-01
6
843 Views
Last Modified: 2012-06-27
In my code I have a form similar to this:

<form action=testpost.jsp method=post onsubmit='postdiv(this);return false'>
      <input name=test><br>
      <input type=submit name="testsname" value="testsvalue">
      <input type=submit name="testsname" value="testsvalue2">
</form>

in the postdiv function, i need to know which of the submit buttons was pressed. I cannot use an onclick method because it would involve too much code rework. I can I find out which button triggered the submit. The browser has to be able to know somehow because it only includes that button with the form on the post. Any help would be greatly appreciated.
0
Comment
Question by:abhutiani
6 Comments
 
LVL 8

Expert Comment

by:plq
ID: 10735428
In IE you might be able to use

event.srcElement

That might return the control that raised the event

Then you can do event.srcElement.id or .name to get the id/name
0
 
LVL 8

Expert Comment

by:plq
ID: 10735436
ps that might also work on moz etc, I've just never tried. thanks
0
 

Author Comment

by:abhutiani
ID: 10735585
event.srcElement only returns the form that was submitted not the submit button that submitted the form.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:lil_puffball
ID: 10736056
plg's solution does work:

<body onclick="alert(event.srcElement.value);">

<form action=testpost.jsp method=post onsubmit='postdiv(this);return false'>
     <input name=test><br>
     <input type=submit name="testsname" value="testsvalue">
     <input type=submit name="testsname" value="testsvalue2">
</form>

However it doesn't work on netscape. You can get the position of the mouse and check if it's over the buttons, but that's usually too much work.
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10736148
Here is a cross-browser example:

<script>
var x,y;
var btns;
var pos=new Array();
if(document.layers){
document.captureEvents(Event.MOUSEMOVE);}
document.onmousemove=getMouse;

function getMouse(e){
  if(document.all){
    x=window.event.x+document.body.scrollLeft;
    y=window.event.y+document.body.scrollTop;
  }else if(document.getElementById||document.layers){
    x=e.pageX;
    y=e.pageY;
  }
}
function getButton(){
for(i=0;i<btns.length;i++){
  if(inRange(btns[i])){return btns[i].value;}
}
return false;
}
function inRange(obj){
  var posSX=0,posSY=0,posEX=obj.offsetWidth,posEY=obj.offsetHeight;
  while(obj.offsetParent){
    posSX+=obj.offsetLeft;
    posSY+=obj.offsetTop;
    obj=obj.offsetParent;
  }
  posEX+=posSX;
  posEY+=posSY;
  if(x>posSX && x<posEX){
    if(y>posSY && y<posEY){
      return true;}}
  return false;
}
</script>

<body onclick="if(getButton()){alert('Button value: '+getButton());}" onload="btns=document.forms[0].testsname;">

<form action=testpost.jsp method=post onsubmit='return false'>
     <input name=test><br>
     <input type=submit name="testsname" value="testsvalue">
     <input type=submit name="testsname" value="testsvalue2">
</form>
0
 
LVL 7

Accepted Solution

by:
brgivens earned 500 total points
ID: 10750248
<script>
  var inputUsed="";
  function postdiv(o) {alert("Submitted from " + inputUsed + ".");}
</script>

<form action=testpost.jsp method=post onsubmit='postdiv(this);return false'>
     <input name=test><br>
     <input type=submit name="testsname" value="testsvalue" onfocus="inputUsed='testsvalue';">
     <input type=submit name="testsname" value="testsvalue2" onfocus="inputUsed='testsvalue2';">
</form>

You can substitute onclick, onmousedown, onmouseenter, onmousemove or onmouseover for onfocus if you want.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now