Solved

Determining submit button pressed without onClick event handler

Posted on 2004-04-01
6
851 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

820 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