0

I have a jQuery script for adding dynamic content inside containerDivs. http://jsfiddle.net/Lijo/srFkJ/2/

This div element structure is added in multiple places in the HTML page. Please click First and Second buttons in the fiddle to see this.

The dynamic script adds a button named “View Recipients” to the HTML. When any of the “View Recipients” button is clicked, a new Div element (childHTML in fiddle code) need to be added to the corresponding logRow div (yellow background). How can we do this?

1

1 Answer 1

2

You can add a live listener to the .viewRecipientsButton, which will trigger even for buttons that didn't exist when the listener was added

$('.viewRecipientsButton').live('click', function() {
    $(this).closest('.logRow').append($(childHTML));
});

Demo

Note that live is deprecated these days, but since you're including jQuery 1.4.1 in your document, I suppose this is a requirement.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What can I use instead of "live" if I am migrating to a newer version?
If you're using jQuery 1.7+, you should switch to .on. $(container).on('click', '.viewRecipientsButton', listener); Note that container has to be a container that does exist when the listener is added, and it should preferrably be one as close to the button as possible. From jQuery 1.4.2 to 1.7, use delegate, with almost exactly the same syntax: $(container).delegate('.viewRecipientsButton', 'click', listener);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.