tags:

views:

39

answers:

4

I have 3 buttons in my form.

All the button actions goes to a same page after clicking it depending upon what button is clicked.

My query is: The name of the buttons are 'add','edit' and 'remove'

When the 'add' button is clicked, using js I need to get the value of that button and similarly for the other two buttons....

Can you please help?

+1  A: 

have you tried: document.getElementById('button_id').value in the js function that you'll call when the button is clicked?

aniri
Personally, i'd vote for a solution that doesn't require 3 separate functions to accomplish this rather simple task.
cHao
+2  A: 

What about:

var buttonValue = document.getElementById('IdOfYourButton').value
ŁukaszW.pl
So, three click handlers, all identical except for this line? There's got to be a better way.
cHao
He said he need to get value of three buttons.. Show me better way with pure javascript? Also he can make one handler method with a buttonId parameter..
ŁukaszW.pl
@LucaszW.pl: check my code.
cHao
A: 
<script language=javascript>
function bevent()
{
var xx = document.xx.btest;
alert(xx.value);
}
</script>

<form name=xx>
<input type=button value="hello" name=btest onMouseOver="bevent()">
</form>
org.life.java
value is the attribute for button that holds its caption.
org.life.java
+2  A: 
<form>
 <script type="text/javascript">
  function doAction(value)
  {
   // Don't really have anything to set...just show the value
   alert(value);
  }
 </script>
 <input type="button" value="Add" onclick="doAction(this.value)">
 <input type="button" value="Edit" onclick="doAction(this.value)">
 <input type="button" value="Remove" onclick="doAction(this.value)">
</form>

There are other ways that involve not even passing the button's value, but they're not as compatible across browsers (IE uses a slightly different event model).

Of course, if you can get by without doing it in Javascript, and can just pass the clicked button to the server, it gets even easier than that...

<form>
 <input type="submit" name="action" value="Add">
 <input type="submit" name="action" value="Edit">
 <input type="submit" name="action" value="Remove">
</form>

and whichever button you click gets put into the url as action=Add or whatever.

cHao