I`m trying to disabled 6 buttons when a condition is met. I have given the buttons the same class. Is there a simplest/shorter way to write ;
var ButtonCollection = document.getElementsByClassName("button");
function PopUp() {
x = L + Y;
var Count = 0;
MonsterDiv2.addEventListener("click", function () {
Count += 1;
if (Count == 2) MonsterDiv2.style.display = "none";
ActionList.innerHTML += `<li>.</li>`;
ButtonCollection[0].disabled = false;
ButtonCollection[1].disabled = false;
ButtonCollection[2].disabled = false;
ButtonCollection[3].disabled = false;
ButtonCollection[4].disabled = false;
ButtonCollection[5].disabled = false;
//
});
}
2
Answers
Hopefully, this is what you are looking for. Also maybe I would suggest replacing the
enableButtons()
function withtoggleButtons()
, it will be more universal.And don’t forget to clean the
EventListener
🙂I guess the first thing you could do is refactoring the
ButtonCollection[<index>].disabled = false;
You could do so by writing the following code:
Note that you have to convert
HTMLCollection
toarray
before using theforEach
method