Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript forEach is not a function

I’m trying to add a click event to all my edit icons. Each icon has an ID that I’m using to call them.

<div><img src="" id="editCatsSVG" /></div>

Here’s my js file below.

const edicatsIcon = document.querySelector('#editCatsSVG'),
      deleteCatsIcon = document.querySelector('#deleteCatsSVG');
      
const catsActionsModal = document.querySelector('#catsActions'),
      closeCatsModal = document.querySelector("#closeModalIConButton");




edicatsIcon.forEach(addEventListener("click", function(){
    catsActionsModal.style.display = "flex";
}));

closeCatsModal.addEventListener("click", function(){
    catsActionsModal.style.display = "none";
    
});

Is there a way I can add the forEach func so when all the editcatsIcon is clicked, the modal shows?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

please?

Many thanks.

>Solution :

const edicatsIcon = document.querySelector('#editCatsSVG') will return one element, not an array of elements. Also, you’re querying for elements with the id editCatsSVG, and you can’t have duplicate ids.

This

edicatsIcon.forEach(addEventListener("click", function(){
    catsActionsModal.style.display = "flex";
}));

Should be

edicatsIcon.addEventListener("click", function(){
    catsActionsModal.style.display = "flex";
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading