Game Development Reference
In-Depth Information
// <span class="wrong">?</span>]
</script>
Used in the game
As mentioned previously, every node selection was done using query selectors in
the game. It is worth mentioning that it is not possible to register an event listener
to a collection of nodes at once. You will need to iterate through the entire list (or at
least a part of it) in order to touch one or more individual nodes in the list.
// Select a collection of zero, one, or more
buttons
var playBtns =
document.querySelectorAll("button[data-intent='play']");
// Assign the same click handler to all of
these buttons
for (var i = 0, len = playBtns.length; i < len;
i++) {
playBtns[i].addEventListener("click",
doOnPlayClicked);
}
// This does not work >> TypeError: Object
[object Array] hasno method 'addEventListener'
playBtns.addEventListener("click",
doOnPlayClicked);
If you attempt to call any function that would normally call on an individual node dir-
ectly on a result set from a querySelectorAll call, you will get a TypeError
since the function called is applied to the array element and not each and all of its
elements.
Search WWH ::




Custom Search