5

So I have a div that contains a number of other divs. Each of these other divs has a class called record. I only use this class to select all of these divs.

<div id = "resultContainer">
   <div class="record">result1</div>
   <div class="record">result2</div>
   <div class="record">result3</div>
   <div class="record">result4</div>
</div>

I also add a click event=

$(".record").click(function(e) {
        do stuff here....
    });

Now I want to dynamically add another div.

$("#resultContainer").append("<div class='record'>result5>/div>");

But now that click event is not added to the record.

My idea was to create a function called update() that executed the $(".record.... code and call the function each time I added a record. But then the original divs do the action more than once!

How do I get all of my divs, regardless of when they were added, to before the do stuff here... exactly once when clicked?

thanks!

In addition, I also have buttons on the dynamic div. So a solution that was able to handle something lik this: $(".save").button({ icons: { primary: 'ui-icon-disk' }, text: false }); would be preferable.

5 Answers 5

7

Don't use .live() in this case. This is a perfect situation for jQuery's .delegate() method, which is more efficient.

$("#resultContainer").delegate('.record','click',function() {
    // do stuff here...
});

Only clicks inside the resultContainer need to be processed to see if they match .record, where .live() will need to process every click on the page.

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

2 Comments

The issue that I did have with this is that I had a lot of events, and the containers that I had to delegate too were too lagre. I had to go back to implementing the events normally and finding a different way to make sure the events were not applied twice. Good for most applications but it was too slow for me.
The problem is that this method does delays the "work" to "runtime" rather than "loadtime". So when I create some new html I would rather spend the time then applying the events to only the tags that it has not been applied to already, rather than spending the time when the user clicks to figure out if an event needs to be fired.
4

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

Attach a handler to the event for all elements which match the current selector, now and in the future.

$(".record").live("click", function(e) {
    //do stuff here... 
});

8 Comments

whats the cost of doing this however?
@kralco One event is attached to document. Check out the docs (api.jquery.com/live) for more info.
is there a way to use this solution in this situation? $(".save").button({ icons: { primary: 'ui-icon-disk' }, text: false });
@kralco For button(), check out this answer: stackoverflow.com/questions/3028912/…. Also, if all your records are in resultContainer, patrick dw is right -- use delegate.
@patrick - the difference being that delegate allows for adding the event to only a parent element, rather than the whole document? so something like .delegate(document,'click',function... would be equivilent to .live('click',function...?
|
2

As of jQuery 1.7 you should use on() instead of live() or delegate(). From the documentation:

As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

The on() method should be attached to a static parent or document:

$(document).on( 'click', '.record', function(e) {
    // do stuff here....
});

Comments

1

Take a look at the jQuery live() function

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

You can add an event listener for all div, regardless of changes in the page

Comments

0

Try live() method. Instead of:

$(".record").click(function(e) {
    do stuff here....
});

Try this:

$(".record").live(function(e) {
    do stuff here....
});

Comments

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.