0

OK, so, I use livequery() to bind a function to the click event of all links of class 'ajaxLink'. The function fires perfectly...once. After the first successful ajax call on a click, subsequent clicks don't fire the ajax, which means (I'm guessing) they aren't being bound by the livequery() code anymore.

I saw where others who had a similar issue moved their code outside the ready() function, so I tried that, to no avail (same results).

$('a.ajaxLink').livequery('click', function(e) {
  e.preventDefault();
  var target = $(this).attr('href') + '&ajax=y';
  var x = $(this).html();

  $.ajax({
   type: 'POST',
   url: target,
   //data: str,
   success: function(msg) {
    $('#mainPanel').slideUp(500, function() {
     $(this).html(msg).slideDown(1000);
    });
   }
  });
 })

Let me know if you need more detail. Thank you in advance for your help! This site is excellent.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
hannebaumsaway
  • 2,644
  • 7
  • 27
  • 37
  • 1
    I'm looking at the plugin page and I'm really trying to figure out what makes `livequery` different from the native `live`. Do you know? – Peter Bailey Aug 26 '10 at 20:38
  • 1
    @Peter - `.livequery()` actively seeks out elements added to the DOM, for events it's less efficient and just filled a gap before `.live()` was there. It's mainly for running plugins and such now, something `.live()`, being event bubble driven, can't do. – Nick Craver Aug 26 '10 at 20:42
  • rhbaum - What version of jQuery are you using? – user113716 Aug 26 '10 at 20:43
  • My script tag is pointed to 'http://code.jquery.com/jquery-latest.pack.js'. – hannebaumsaway Aug 26 '10 at 20:47

1 Answers1

0

.livequery() has issues depending on which version and some special DOM cases. However, you can ignore that problem...since you're doing something that works off events, you can use the built-it .live() here (available in jQuery 1.3+), like this:

$('a.ajaxLink').live('click', function(e) {
  e.preventDefault();

  $.ajax({
    type: 'POST',
    url: $(this).attr('href') + '&ajax=y',
    //data: str,
    success: function(msg) {
      $('#mainPanel').slideUp(500, function() {
        $(this).html(msg).slideDown(1000);
      });
    }
  });
});
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Worked, thanks! The funny thing is, I originally used live(), and had the same problem I described above, which prompted me to search for and find livequery(), which actually worked for a time. Then I had to switch my site to a new domain, and got the problem once again. Funny that switching back to live() solved the issue in the end! Thanks again, all who commented. – hannebaumsaway Aug 26 '10 at 20:49