Javascript getting index of the DOM element

javascript get index of element in parent
indexof javascript
javascript indexof object
get index of clicked element javascript
javascript find index of object in array
nodelist indexof
jquery get index of matched element
javascript get index of li in ul

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

HTMLCollection(2) [button.mybutton, button.mybutton]

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

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

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>

JavaScript DOM: Find Element Index In Container, Get the HTML content of the first <p> element (index 0) inside the document: The item() method returns a node at the specified index in a NodeList object. If the item is present more than once, the indexOf method returns the position of the first occurence. Note: The first item has position 0, the second item has position 1, and so on. Tip: If you want to search from end to start, use the lastIndexOf () method. Browser Support. The numbers in the table specify the first browser version that fully

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

function handleClick(event) {
  console.log(;  // 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( // prints the id of the clicked button

HTML DOM item() Method, Example. Get the index of the first element in the array that has a value of 18 or more: var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to get the index of the element in javascript?

  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").


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


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

for (let i = 0; i < myButtons.length; i++) {
  myButtons[i].addEventListener("click", function(event) {

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) {

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) {

JavaScript Array findIndex() Method, If .index() is called on a collection of elements and a DOM element or jQuery object is operation to .get() , which accepts an index and returns a DOM node, .​index() can take a <script src=""></script>​. If a selector string is passed as an argument, .index () returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector. If the element is not found, .index () will return -1. The complementary operation to .get (), which accepts an index and returns a DOM node, .index

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 ([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.

Array.prototype.indexOf(), Example. Get the index of the first element in the array that has a value of 18 or more: var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; Finding HTML Elements by CSS Selectors If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method.

1.0.index( element ), Name HTMLCollection.item( ): get an element by position — DOM Level 2 HTML Synopsis Selection from JavaScript: The Definitive Guide, 5th Edition [Book] In straight up javascript (i.e., no extensions such as jQuery, etc.), is there a way to determine a child node's index inside of its parent node without iterating over and comparing all children no

JavaScript Array findIndex() Method, For more in-depth JavaScript knowledge, read our book, JavaScript: Novice to It's very useful for multi-element DOM manipulation, as well as every <div> element on a web page and outputs the index and the ID of each of them: this means we can get an element's href attribute by writing this.href . In the HTML DOM, the Element object represents an HTML element, like P, DIV, A, TABLE, or any other HTML element. Properties and Methods. The following properties and methods can be used on all HTML elements: Property / Method. Sets or returns the accesskey attribute of an element. addEventListener () Attaches an event handler to the specified

HTMLCollection.item( ): get an element by position, Examples of how to do query, style, dom, ajax, event etc like jQuery with plain javascript. Get the current vertical position of the scroll bar for the element. Definition and Usage. The getAttribute() method returns the value of the attribute with the specified name, of an element. Tip: Use the getAttributeNode() method if you want to return the attribute as an Attr object.

  • 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.
  • in the function that gets executed on click, you can pass an event argument and in the function code you may use to get the id
  • 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, check my answer