SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .live vs .delegate

    Based on a recommendation from people a lot smarter than me, I tried delegate and am evidently doing something wrong because it is not working as intended. I have a situation where I am refreshing data with ajax and want my click and other events to be attached to the refreshed data as well as the original page data.

    With the code below, .live works great, going from page to page. But when I use the delegate in it's place, it works on the loaded page, but not on the subsequent agax refreshed data. Am I doing something wrong?

    Code:
    $('.likegrp .likes a, .likegrp .dislikes a, .likegrp .follow a').live('click', function(e) {
    Code:
    $('.likegrp .likes, .likegrp .dislikes, .likegrp .follow').delegate('a', 'click', function(e) {

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Are the elements you are targeting in the delegate call themselves being refreshed?

    Delegate should be placed on the parent of any element that is being refreshed. E.g.

    Code:
    $("#maincontent, #sidebar").delegate(".like a, .rating a", "click", funtion(e) {
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried it both ways. I replace pretty large chunks of the page, so if I go up, do I need all elements between the closest one that doesn't get changed and the one I am acting on even if there are multiple events in between? Or can I just select the top one and then enough under that to identify it? Does that make sense.

    I was doing some reading and came across a statement that sounded like what you are saying. Does .live work differently in that sense because live works fine with those tags?

    Also, did you see my other question on the post the other day about the create: function that wasn't attaching after I changed data? Is that something that I would do with delegate also? Sorry for crossing two posts but still don't have an answer to that one.

    I really appreciate the help.

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe a different way to ask the question is how do you know how far up the tree to go in any situation with selectors? I assume you only need to go far enough to uniquely identify the object. So if I have an 'a' and a class parent that uniquely identifies that 'a' but both are being replaced with the ajax data, do I need all the intermediary selectors on the way up to get the one that doesn't change?

    Or do I put all the selectors that change on the right side of the argument. ahhhhhh let me try that.

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did it. I put the selector that doesn't change on the left and the identifying selectors on the right side of the argument and it works great. Now it makes perfect sense. Thanks again for the help.

  6. #6
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Glad to see you figured it out!

    The create function would need to be called whenever you inject new content, so indeed inside of the delegate callback would be the best place for it.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  7. #7
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, one of those aha moments. Not sure I understand the function side but let me play with it and see if I can get it.

    Rather than erase, another aha moment so I went and put the call inside the delegate and it works like a champ. Thanks so much for the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •