Skip to content
Advertisement

How to add event listener to all elements

I am generating input fields and a font awesome icon dynamically via JavaScript. I want to attach an event that alerts a string to each created icon, currently the event works only for the first created icon, how can I attach the event to all icons? Here is my code:

JavaScript

Advertisement

Answer

You need to loop over the elements (you should have an error on your console).

Instead of

JavaScript

you should use

JavaScript
Advertisement