Solved

Determining submit button pressed without onClick event handler

Posted on 2004-04-01
6
847 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…

772 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