-1

I am trying to implement a click function inside a div with some nested children tags such as span, td, etc. However, these nested tags are loaded dynamically, mostly using ajax(). The returned result is displayed using .html(data) function. However, once the data is changed and new tags are added, the old javascript written to detect the clicks no longer work.

I want to know if there is a way to make this work?

An example of what i am talking about can be found here.

1

2 Answers 2

3

You are supposed to attach the event handler on the wrapper element like so:

http://jsfiddle.net/V4Sfw/1/

$("#testing").on("click", "span", function() {
    alert("now?");
});
$("#testing").html("<span>How about now?</span>");
Sign up to request clarification or add additional context in comments.

Comments

1

You could use live to attach handlers that always work, as long you know the structure of the loaded HTML.

2 Comments

This is true but .live() has been deprecated for a long time. The current release uses a 3-argument version of .on().
That seems to be right. It's been a while since used jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.