Good, a question has arisen with the function for...in
of javascript, and I do not know if it is because I am using it badly, but it does not work for me. I am trying to apply to several elements of the DOM with querySelectorAll
and I try to apply to all elements the style with for...in
in this way;
var capas = document.querySelectorAll('div');
for (var i in capas) {
capas[i].style.transition = 'all 2s';
}
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
The console tells me that capas[i].style
is undefined and I do not understand why. The problem I solved with the method forEach()
capas.forEach(function (capas) {
capas.style.transition = 'all 2s';
});
so my question is regarding the use of for...in
(which I have not seen anywhere that is obsolete).
Am I misusing the sentence or am I misusing it? Why are you checking undefined the elements?