I would like to define variables within scope of paginate()
. However, any variable defined with this
(e.g. this.parentlist
) is not available within the jQuery click
method
var paginate = function(elt) {
//would like to define variables within scope of paginate()
//however, any variable defined with this (e.g. this.parentlist) is not available within the jQuery click method
this.parentlist = elt.find('ul');
var listitems = elt.find('li');
this.listcount = this.list.find('li').size();
var showitems = '3';
this.numberofpages = ~~((this.count / this.show) + 1);
this.parentlist.after('<ul class="links"></ul>');
for(i=1;i<=this.numberofpages;i++) {
$('.links').append('<li><a href="#'+i+'">'+i+'</a></li>')
};
//click a link
$('.links a').click(function() {
//this.parentlist is not available here
listitems.hide();
this.start = listitems.index() * showitems;
this.end = (this.start + 1) * showitems;
this.selected = listitems.slice(this.start,this.end).show().addClass('selected');
console.log(this.selected);
console.log(this.parentlist);
});
};
paginate($('.pages'));