1

I have two submit buttons with the same name but submitting different values in my html form, how can know which submit button was pressed when any one of them is pressed using javascript...? like,

<input type="submit" name="button1" value="1" />
<input type="submit" name="button1" value="2" />
1
  • In your click event handler use this.value and see if it's 1 or 2 Commented Oct 20, 2011 at 16:01

4 Answers 4

1

If you dont want to use jquery you just can try using onclick event:

<input type="submit" name="button1" value="1" onclick="showValue(this)"/>
<input type="submit" name="button1" value="2" onclick="showValue(this)"/>

Then in javascript:

function showValue(obj) {
   alert(obj.value);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Give them different ids and check for the id with

 this.id;

1 Comment

Could you please tell how can i check for the id of the clicked button..?
0

In jQuery this could be like this:

First set the same class for both buttons:

<input type="submit" name="button1" value="1" class="myclass"/>
<input type="submit" name="button1" value="2" class="myclass"/>

Then in javascript:

$("input .myclass").click(function() {
   var value = $(this).val();
});

1 Comment

the code you have given is jQuery, can you give a javascript code for the same?
0

One not very elegant solution - but still one possibility - is to use a hidden and two or any quantity of button-type inputs.

configure onclick to set the hidden and then submit the form.

<form id="f1">
   <input type="button" onclick="setAndSubmit(1)" value="send 1" /> 
   <input type="button" onclick="setAndSubmit(2)" value="send 2" /> 
   <input type="hidden" name="fakebutton" id="fakebutton"/>
</form>

<script type="text/javascript">
    function setAndSubmit(n){
       $('#fakebutton').val(n);
       $('#f1').submit();
    }
}

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.