1

We have multiple forms with different IDs but same onclick function .

For Like ,

<input type="button" id="a"  value="SUBMIT"  onclick="fnSubmitForm();">

<input type="button" id="b"  value="SUBMIT"  onclick="fnSubmitForm();">

<input type="button" id="c"  value="SUBMIT"  onclick="fnSubmitForm();">

How to find the ID of which submit button is submitted.

CodeMan
  • 1,941
  • 6
  • 26
  • 44

3 Answers3

2

add the function in the onclick like this

<input type='button' id='a' value='submit' onclick='fnSubmitForm()'/>
<input type='button' id='b' value='submit' onclick='fnSubmitForm()'/>
<input type='button' id='c' value='submit' onclick='fnSubmitForm()'/>

then get the passed value using the following snippet.

function fnSubmitForm(){
    console.log(this.document.activeElement.getAttribute("id"));
}
Sarim Javaid Khan
  • 810
  • 15
  • 30
1

Pass in this to the function:

onclick="fnSubmitForm(this);"

and you can pick up the id:

function fnSubmitForm(el) {
  console.log(el.id);
}

DEMO

EDIT

Ok, since you can't edit the HTML, here's a script only solution:

// pick up the input elements with type=button
var buttons = document.querySelectorAll('input[type="button"]');

// add click events to each of them, binding the function
// to the event
[].slice.call(buttons).forEach(function (el) {
  el.onclick = fnSubmitForm.bind(this, el);
});

function fnSubmitForm(el){
  console.log(el.id);
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

Try like this

onclick="fnSubmitForm(this.id);"

and get the value with the first function argument

function fnSubmitForm(id) {
  //your code
}