1

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?

2
  • What do you mean with "button actions"? Forms have actions, buttons do not. Are you posting to a server-side script? Can you show some code? Commented Aug 9, 2010 at 10:51
  • Is this ajax related, or would you be fine with just submitting the form to the server and having the server redirect you? Commented Aug 9, 2010 at 10:57

4 Answers 4

8
<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.

Sign up to request clarification or add additional context in comments.

1 Comment

Short and sweet. Probably still the best answer.
6

What about:

var buttonValue = document.getElementById('IdOfYourButton').value

2 Comments

So, three click handlers, all identical except for this line? There's got to be a better way.
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..
1

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

1 Comment

Personally, i'd vote for a solution that doesn't require 3 separate functions to accomplish this rather simple task.
1

There is target property which you can use.It targets the element which caused the event to occur.

button.addEventListener('click',function(e){
     console.log(e.target.value);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.