Reputation: 340
I have this bit of code, how would I simplify it into a for each loop using two arrays.
listLength = $('.list-items li').size();
if (listLength >= 4){
$('.pagination ul li:lt(3)').addClass('selected');
}else if (listLength >= 3){
$('.pagination ul li:lt(5)').addClass('selected');
}else if (listLength >= 2){
$('.pagination ul li:lt(7)').addClass('selected');
}else if (listLength >= 1){
$('.pagination ul li:lt(9)').addClass('selected');
}else if (listLength >= 0){
$('.pagination ul li:lt(11)').addClass('selected');
} else{
}
I know I can do something like this...
list = $('.list-items li');
page = $('.pagination ul li');
$.each(list, function(i, item) {
//do something here
});
or ...
var i;
list = $('.list-items li');
for (i = 0; i < list.length; ++i) {
//do something here
}
but I'm unsure how.
Upvotes: 0
Views: 68
Reputation: 25634
Try this :
listLength = $('.list-items li').size();
var temp = 11 - 2 * Math.min(listLength,4);
$('.pagination ul li:lt('+ temp +')').addClass('selected');
Upvotes: 1