Tomas Raies
Tomas Raies

Reputation: 53

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?

Upvotes: 1

Views: 69

Answers (2)

Taha Paksu
Taha Paksu

Reputation: 15616

For an example:

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

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

Upvotes: 0

gdoron
gdoron

Reputation: 150263

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

Upvotes: 4

Related Questions