Reputation: 16489
I have many select all
buttons within my HTML that essentially do the same thing. However they all have different ids because the buttons represent different divs
that have different content within them. I am new to JS and I was wondering how can I perform the same logic across many select all
buttons when one of them is clicked?
I thought about putting each select all
button in an array containing all the ids and using
arr = [id1,id2,id3];
for (var i = 0; i < arr.length; i++) {
var k = document.getElementById(arr[i]);
k.onclick = function() {//...logic...}
}
I'm new to JS so I'm not sure if this is a good method to approach
Upvotes: 0
Views: 121
Reputation: 33409
Your current code should work. Just make sure that in the array of IDs, they are proper strings (put quotes around them). Also, if you plan on doing something with i
inside the click callback, please read How do JavaScript closures work?
Upvotes: 1