//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
});
}
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")})});
const arrayOfElements = document.querySelectorAll(".classNameToAddOnClickTo")
arrayOfElements.forEach(x => x.setAttribute("onclick", "methodToCall(this.id)"))