Skip to content
Advertisement

No events for dynamically generated input tags

I have dynamically generated some input tags for a web application.

JavaScript

The appended elements do not seem to have associated click, select etc.. events. I read you can you .on(). I would like to associate all possible events to all types of elements in a general way. What is the best way to go about this?

Advertisement

Answer

Suppose you want to assign a default behavior on click event for all inputs with a specific class, say ‘foo’:

JavaScript

If you don’t go this way and try the following:

JavaScript

then the behavior will be added only to existing elements, not to those added after the script executed.

Advertisement