Search code examples
javascriptjqueryon-the-fly

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


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?


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