I am new here (and new to JavaScript), so please excuse my super basic questions. I have a HTML page with different images that all share a class on it. By using getElementsByClassName, I get an array. I want to add an event listener to each of the cells in the array by using the .map() function.
This is what I have:
window.onload = function(){
var allImgs = document.getElementsByClassName("pft");
var newImgs = allImgs.map(
function(arrayCell){
return arrayCell.addEventListener("mouseover, functionName");
}
);
};
This keeps showing the error "allImgs.map is not a function" even when I change the inner function to something that doesn't include the event listener.
I have another version of this code where I just loop through the array cells within window.onload and add the event listener to each of them that way and it works. Why isn't the .map() function working? Can it not be used in window.onload?
The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array. To solve the error, console. log the value you're calling the map() method on and make sure to only call the map method on valid arrays.
map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.
The map() method calls a callback function on every element of an array and returns a new array that contains the results. The map() method takes two named arguments, the first one is required whereas the second one is optional.
ES6 - Array Method map() map() method creates a new array with the results of calling a provided function on every element in this array.
getElementsByClassName()
returns an HTMLCollection
not an Array
. You have to convert it into a JavaScript array first :
allImgs = Array.prototype.slice.call(allImgs); // or allImgs = [].slice.call(allImgs); // or (thanks @athari) allImgs = Array.from(allImgs); // or (thanks @eliaz-bobadilla) allImgs = [...allImgs]
By using getElementsByClassName, I get an array.
No, you don't.
You get a live HTMLCollection. This is array-like but is not an array.
Since it is sufficiently array-like, you can apply the map
method from a real array.
var text_content = [].map.call( document.getElementsByClassName("sdf"), function (currentValue, index, collection) { return currentValue.innerHTML; } ); console.log(text_content);
<p class="sdf">foo</p> <p class="sdf"></p> <p class="sdf">bar</p> <p class="sdf"></p> <p class="sdf"></p>
Another option would be to use map
directly:
[].map.call(allImages, function() { ... });
However, what you are doing is better achieved with Array.prototype.forEach
.
Compact syntax:
[...document.getElementsByClassName("pft")].map(arrayCell => arrayCell.addEventListener("mouseover", "functionName"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With