how should I handle an event for an element that has not yet been loaded when the page is loaded

StackOverflow https://stackoverflow.com/questions/9864533

  •  26-05-2021
  •  | 
  •  

Question

I'm kind of a noob in jquery, so i'm sorry if the question is a little obvious.

I wondered how should I handle an element which is created using the .html() jquery method, so there is no way to handle it after $(document).ready. Is there anyway to create the handler when the element is created or something?

Était-ce utile?

La solution

Simple as that:

$('#containerId').on('eventType', 'childSelector', handler);

Always bind the delegate event to the closest static element of the dynamic elements.

If you want to understand how this magic happens, read the on docs

Autres conseils

For an example:

$("#mydiv").html("<span>Content</span>");
$("#mydiv span").css("background-color","blue");

so, call it after you set the .html().

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top