1
So I’m trying to select all the existing images and I came to develop this code
(function ($) {
var elementsImg = document.getElementsByTagName('img'), len, i;
for (i=0, len=elementsImg.length; i<len; i++) {
elementsImg[i].setAttribute('loading','lazy');
}
})(jQuery);
However, in the devtools console it appears as
I would like to know a possible solution and what I am doing wrong.
You’re not doing anything wrong. The method
setAttribute
there is no return, so it appearsundefined
.– Cmte Cardeal
inspecting the images, they have the modified attributes?
– André Walker
@Andréwalker Yes, the attributes were modified but I thought that using the console.log the way I did above it would return the img elements that were added the attribute "loading". However now I understood by the explanation above and below why it is returning "Undefined".
– Arthur Davi Borba Duarte