-2

Can you help me with the method to find the id of the DOM button clicked

out=document.getElementsByClassName("mybutton")
HTMLCollection(2) [button.mybutton, button.mybutton]
0
:
button.mybutton
1
:
button.mybutton
length
:
2
__proto__
:
HTMLCollection
2
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Sep 14, 2018 at 10:48
  • 1
    in the function that gets executed on click, you can pass an event argument and in the function code you may use event.target.id to get the id Commented Sep 14, 2018 at 10:49

4 Answers 4

1

Add a click handler to the buttons with buttonElement.addEventListener('click', clickHandlerFunction);.

function onMyButtonClick(clickEvent) {
  var button = clickEvent.target;
  console.log('ID of clicked button: ' + button.id);
}

document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.mybutton').forEach(function (button) {
    button.addEventListener('click', onMyButtonClick);
  })
});
<button id="button-1" class="mybutton" type="button">Button 1</button>
<button id="button-2" class="mybutton" type="button">Button 2</button>

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

Comments

0

If your buttons looks something like this,

<button type="button" id="save" class="button">Save</button>
<button type="button" id="cancel" class="button">Cancel</button>

You can listen for the click events on the button and get its id using event.target.id

function handleClick(event) {
  console.log(event.target.id);  // prints the id of the clicked button
}

document.querySelectorAll(".button").forEach(function(button) { 
  button.addEventListener('click', handleClick);
});

ES6 approach:

const buttons = document.querySelectorAll(".button");
Array.from(buttons, button => button.addEventListener('click', () => {
    console.log(button.id) // prints the id of the clicked button
}));

Comments

0
  1. Select all buttons in document.
  2. Use for or forEach to iterate over list of buttons and add event listener to Each of the buttons.
  3. Use getAttribute() method to get specific attribute of element(for example "id").

html:

<button class="mybutton" id ="one">1</button>
<button class="mybutton" id="two">2</button>
<button class="mybutton" id="three">3</button>

javascript:

let myButtons = document.getElementsByClassName("mybutton");

for (let i = 0; i < myButtons.length; i++) {
  myButtons[i].addEventListener("click", function(event) {
    event.preventDefault();
    console.log(this.getAttribute("id"));
  });
}

We can also use forEach method but because document.getElementsByClassName returns a HTMLCollection, not an array, with ES6 Array.from() or spread which builds arrays from array-like objects we can use forEach method to iterate over HTMLColection objects. more details in here

[...myButtons].forEach(function(button) {
  button.addEventListener("click", function(event) {
    event.preventDefault();
    console.log(this.getAttribute("id"));
  });
});

Or you can use document.querySelectorAll() that return NodeList instead of document.getElementsByClassName().

let myButtons = document.querySelectorAll(".mybutton");

myButtons.forEach(function(button) {
  button.addEventListener("click", function(event) {
    event.preventDefault();
    console.log(this.getAttribute("id"));
  });
});

Comments

-1

Just use for loop, and then check attribute of that element:

<button onClick="buttonClick($event)" id="something1" class="mybutton"></button>
<button id="something2" class="mybutton"></button>
<button id="something3" class="mybutton"></button>

function buttonClick(event) {
 for(var i = 0; i < out.length; i++) {
  if (event.target.attribute[0] === out[i].attributes[0].value){ 
  do something}
 }
}

this is if you need to check something, but click function alone with event will check which button is clicked.

6 Comments

How would this find the ID of the clicked element?
it would log the id of that element
Sorry , I meant the Index of the Dom element clicked . is there a method please
@NemanjaG This would log the ID of all of the buttons. Not the clicked button.
@Turnip this will check ID of clicked element, just pass event on click and use event.target, check my answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.