John Rand
John Rand

Reputation: 1034

Using jQuery to fire an event on content just added by jQuery

I have a jQuery script which is triggered by an onClick event ( through $(".addContent").click(function(){ ) and which updates the page, adding an input text tag.

I am subsequently trying to trigger another script using an onChange event ( through $(".guestInput").change(function(){ ) on the input text tag which was just added, once the viewer makes a change. It doesn't work.

I have tried adding the input text tag as static html, and the subsequent onChange event fires successfully, but not when I add the field through jQuery.

Hope this is clear.

Any idea as to what I am doing wrong?

Thanks,

John

Upvotes: 0

Views: 54

Answers (1)

David Houde
David Houde

Reputation: 4778

You need to use live() for DOM elements added after the initial page load.

so .click(function() { do_something; });

would now be .live("click", function() { do_something; });

see: http://api.jquery.com/live/

Description: Attach an event handler for all elements which match the current selector, now and in the future.

Upvotes: 3

Related Questions