select element with same id in javascript

It is not recommended to have multiple elements with the same id in HTML. However, if you want to select all the elements with the same id using JavaScript, you can use querySelectorAll method to select all the elements with that particular id and loop through them to perform any operations.

Here's an example code snippet that selects all the elements with the same id "example_id" and logs their innerHTML values to the console:

const elements = document.querySelectorAll("#example_id");

for(let i = 0; i < elements.length; i++) {
  console.log(elements[i].innerHTML);
}
143 chars
6 lines

Alternatively, you can also use getElementById method to select only the first element with that particular id:

const element = document.getElementById("example_id");

console.log(element.innerHTML);
88 chars
4 lines

gistlibby LogSnag