Solved

Determining submit button pressed without onClick event handler

Posted on 2004-04-01
6
839 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now