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
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

For an example:

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top