javascript - pagination called to multiple elements not working, only calls it for the last one -


i've got pagination script when called multiple times, fires last element has been called. ideas why happening?

<script> $('.calendar-nagyhaz').scrollpagination(); $('.calendar-felso').scrollpagination(); </script> 

if try call ".calendar-felso" affected. pagination script looks this:

(function($) {  $.fn.scrollpagination = function(options) {      var settings = {          nop     : 1, // number of posts per scroll loaded         offset  : 0, // initial offset, begins @ 0 in case         error   : '', // when user reaches end message                                     // displayed. can change if want.         delay   : 500, // when scroll down posts load after delayed amount of time.                        // usability concerns. can alter see fit         scroll  : false // main bit, if set false posts not load user scrolls.                         // still load if user clicks.     }      // extend options work plugin     if(options) {         $.extend(settings, options);     }      // variables      $this = $(this);     $settings = settings;     var offset = $settings.offset;     var busy = false; // checks if scroll action happening                        // don't run multiple times      function getdata(add) {              if (add == true) {offadd = offset+$settings.nop; } else { offadd = offset-$settings.nop; }             offset = offadd;             lakas = $this.attr("data-lakas");             alert(lakas);              // post data ajax.php             $.post('ajax.php', {                  action        : 'scrollpagination',                 number        : $settings.nop,                 offset        : offset,                 lakas         : lakas              }, function(data) {                      // append data content div                     $this.find(".calendar").empty().append(data);                      // no longer busy!                       busy = false;              });              $.post('month.php', {                  action        : 'scrollpagination',                 number        : $settings.nop,                 offset        : offset              }, function(data) {                      offset = offadd;                      // append data content div                     $this.find('.cal-month').empty().append(data);                      // no longer busy!                       busy = false;              });          }             //run first time         getdata();          // content can loaded clicking loading bar/         $this.find('.cal-prev').click(function() {              if(busy == false) {                 busy = true;                 getdata(false);             }          });          $this.find('.cal-next').click(function() {              if(busy == false) {                 busy = true;                 getdata(true);             }          }); }  })(jquery); 

you setting global variables here:

$this = $(this); $settings = settings; 

it means:

window.$this = $(this); window.$settings = settings; 

what intended is:

var $this = $(this); var $settings = settings; 

that's 1 bug, bug there may more.


here too, these globals:

        if (add == true) {offadd = offset+$settings.nop; } else { offadd = offset-$settings.nop; }         offset = offadd;         lakas = $this.attr("data-lakas"); 

offadd, offset, lakas, these need var in front of them make them local variables.

when have 2 instances of function, overwrite each other's variables. (because globals)


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -