Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add click event to multiple items JavaScript

//HTML
 <body>
    <h1 id="title">Drum Kit</h1>
    <div class="set">
      <button class="w drum">w</button>
      <button class="a drum">a</button>
      <button class="s drum">s</button>
      <button class="d drum">d</button>
      <button class="j drum">j</button>
      <button class="k drum">k</button>
      <button class="l drum">l</button>
    </div>

    <script src="index.js"></script>
  </body>

//JavaScript
//number of elements with class of drum
var numOfDrumButtons = document.querySelectorAll('.drum').length;
//assigning the buttons to an array called drums
var drums = document.querySelectorAll('.drum');
//using a for loop to iterate through each arrayed element
for (var i = 0; i < numOfDrumButtons; i++) {
  //each array indexed and assigned an anonymous function with alert
  drums[i].addEventListener('click', function () {
    alert('I got clicked');//for testing
    //add actions to occur upon click hear
  });
}
Comment

javascript addeventlistener click multiple elements

let all_btn = document.querySelectorAll("button");
all_btn.forEach(function(btn) {
    btn.addEventListener("click", function() {
        console.log(this.innerHTML + " is clicked")
    });
});

// one line code
// let all_btn=document.querySelectorAll("button");all_btn.forEach(function(n){n.addEventListener("click",function(){console.log(this.innerHTML+" is clicked")})});
Comment

javascript add onclick to multiple elements

const arrayOfElements = document.querySelectorAll(".classNameToAddOnClickTo")
arrayOfElements.forEach(x => x.setAttribute("onclick", "methodToCall(this.id)"))
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular number validation 
Javascript :: clear cache javascript 
Javascript :: values javascript 
Javascript :: get id javascript 
Javascript :: react sass 
Javascript :: object length 
Javascript :: click function in js 
Javascript :: why we import react from react 
Javascript :: javascript document get by attribute 
Javascript :: name function in javascript 
Javascript :: .remove javascript 
Javascript :: type conversion in javascript 
Javascript :: convert excel file to json using node js 
Javascript :: An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist. 
Javascript :: splice state react 
Javascript :: api integration for web pages in next js 
Javascript :: javascript promise methods 
Javascript :: jquery sweet popup 
Javascript :: cheerio each 
Javascript :: javascript shell 
Javascript :: arrow function syntax vs function expression syntax 
Javascript :: react custom hooks 
Javascript :: jquery padding top 
Javascript :: change one element in array javascript 
Javascript :: uppercase each word javascript 
Javascript :: java.lang.UnsupportedOperationException: JsonObject 
Javascript :: Is Even 
Javascript :: url 
Javascript :: loop number in react 
Python :: django EMAIL_BACKEND console 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =