Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Determining submit button pressed without onClick event handler

Posted on 2004-04-01
6
Medium Priority
?
868 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

885 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