I'm just asking myself what is the best way between these two codes to get all element without getting a JS exception.
#1
var items = document.querySelectorAll('.items');
if (!!items) {
items.forEach(function(item) {
item.addEventListener('click', function() { ... })
}
}
#2
var items = Array.prototype.slice.call(document.querySelectorAll('.items'), 0);
if (items.length > 0) {
items.forEach(function(item) {
item.addEventListener('click', function() { ... })
}
}
Thank you in advance for your anwsers :)