How to addEventListener to multiple elements in a single line

Example 1:

element1.addEventListener("input", function() {
this function does stuff 

Example 2:

element1 && element2.addEventListener("input", function() {
this function does stuff

It might not be correct grammatically, but is there a way I can give two elements the same event listener at the same time (same line) instead of having to write them apart?

11 thoughts on “How to addEventListener to multiple elements in a single line”

  1. The easiest way so far I’ve learned.

    // Get an array of buttons from the page
    var buttons = document.querySelectorAll(".btns");
    // Loop through the resulting array
    for(var i = 0; i < buttons.length; i++){
      buttons[i].addEventListener("click", function() {
        console.log("Hello World");
  2. Example for initializing one unique event listener specific to each element.

    You can use the slider to show the values in realtime, or check the console.

    On the <input> element I have a attr tag called data-whatever. You can use that to customize each event listener further.

    sliders = document.querySelectorAll("input");
    sliders.forEach(item=> {
      item.addEventListener('input', (e) => {
        console.log(`${item.getAttribute("data-whatever")} is this value: ${}`);
        item.nextElementSibling.textContent =;
    .wrapper {
      display: flex;
    span {
      padding-right: 30px;
      margin-left: 5px;
    * {
      font-size: 12px
    <div class="wrapper">
      <input type="range" min="1" data-whatever="size" max="800" value="50" id="sliderSize">
      <input type="range" min="1" data-whatever="OriginY" max="800" value="50" id="sliderOriginY">
      <input type="range" min="1" data-whatever="OriginX" max="800" value="50" id="sliderOriginX">
  3. Example:

    const element1 = document.querySelector("#element1");
    const element2 = document.querySelector("#element2");
    [element1, element2].map(element => element.addEventListener("click", function() {
      /*some expressions :)*/
  4. Well, if you have an array with the elements you could do:

    let elementsArray = document.querySelectorAll("whatever");
    elementsArray.forEach(function(elem) {
        elem.addEventListener("input", function() {
            //this function does stuff
  5. If you have a DOM Collection, I suggest you to use the for ... of

    In this MDN web doc you can see the details, but, for example, if you have:

    HTMLCollection(6) [a.example, a.example, a.example, a.example, a.example, a.example]

    You can:

    let arrayElements = document.getElementsByClassName('example');
    for (let element of arrayElements) {
        element.addEventListener("click", function() {
            console.log('Whoa! You clicked me')

    And ta-dah! 😉

  6. Event Bubbling is the important concept in javascript, so if you can add event on DOM directly, you can save some lines of code, no need for looping :

    document.addEventListener('click', function(e){
       alert('BUTTON CLICKED');
  7. If you don’t want to have a separate elementsArray variable defined you could just call forEach from an unnamed array with the two elements.

    [ Element1, Element2 ].forEach(function(element) {
       element.addEventListener("input", function() {
          this function does stuff
  8. Here’s what I used to set a click evenhandler on every span in my HTML (each span contains an emoji). When you click it, it will alert the emoji in the sample code.

    Array.from(document.querySelectorAll("span")).map(element => element.addEventListener("click", function() {
    div{background: whitesmoke;}
  9. If you are using Javascript through Electron and you have a list of buttons, you can use this code to add an EventListener to each button.
    I’m actually using this method because classical Javascript methods (map(), forEach() …) weren’t supported anymore.

    let buttons = document.getElementsByClassName('className');
    for(let i = 0; i<buttons.length; i++){
       buttons[i].addEventListener('click', () => {
          /*put your code here*/

Leave a Comment