I don't know if anyone would notice if I edit my previous post so I'm here now to post a new one.
I have this code
$(document) .ready(function(){
var a;
var b;
var c;
var d;
var pmmain =["Citrix", "Coach", "Disney", "Edison", "Eversource", "Fedex", "General Dynamics", "Hertz", "Kimco", "Nabors", "Starbucks", "Timewarner", "TW Telecom", "Weatherford", "Western Union", "Zoetis"];
$("#mains") .one("click",function(){
c=0;
d=c+4;
a=4;
b=0;
for (a=c; a<d; a++){
$("#pm-page-main") .append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">"+pmmain[a]+"</span></div><BR><BR><img src=\"imgs\\"+pmmain[a]+".png\"></div>");
}
});
$("#next-four").click(function() {
b=a;
d = b +4;
$("#pm-page-main") .html("");
for (b=a; b < d; b++) {
if(b != 16){
$("#pm-page-main") .append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[b] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[b] + ".png\"></div>"+b+"").hide().show();
}
else{
a=0;
}}
a = a+4;
});
Code that I've added:
$("#last-four").click(function() {
b = (a)<pmmain.length?a:0;
a = b; // just in case if b is reset to 0
d=b-8;
$("#pm-page-main") .html("");
for (b=a; b-4 > d; d++) {
$("#pm-page-main") .append(d+"<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[d] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[d] + ".png\"></div>").hide().fadeIn(75);
}
a = a-4;
});
});
It's working as it should. It is a menu that shows 4 items at a time. The problem in here is that the for statement doesn't stop looping even when it reaches the end of the array. I tried to put an if statement inside the for loop but to no avail.
My question is how can I stop the for loop when it reaches the end of the array? Or is there a way to get back to the first 4 items. And is there any better way to approach this? I have read about .each in Jquery but i have no idea on how to use it.
UPDATE: I only paste the html where the code is needed.
<div id="pm-page">
<div id="top-left">
<span class="title">PAGEMAKEUP Section</span>
</div>
<div id="top-right">
<button class="btn-home">Home</button> <button class="btn-akbar">akbar</button>
</div><BR>
<h1 class="line-blank"><span></span></h1>
<!--<div class="bar"></div>-->
<div id="pm-body">
<div class="pm-page-menu">
<button id="mains">STYLE TEMPLATES</button>
<button id="next-four">OVERPRINT VS KNOCKOUT</button>
<button>CREATING CUSTOM QR CODE</button>
<button>PM FILE MANAGER</button>
</div>
<div id="pm-page-main">
</div>
</div>
</div>
2nd UPDATED: Hi I hope someone would see this. I have updated the javascript codes and is now looping back to 0. The problem that I can't solve now is that when b is equal to 16, a should reset to 0 so that it would show the first 4 items again. But the problem is that when I click the button again (after the last value in the array), it does not go back to 0 but instead shows an undefined 17,18,19. When I click again, the items 0,1,2 and 3 are skipped and the program jumps to 4,5,6,7 immediately. As of the moment Im still trying to figure it out. Any help would be much appreciated.
3rd UPDATE: Is anyone seeing this? I've inserted a few lines in. It's a new button wherein it will show the previous 4 items. It's working fine except if I'm at the first four items. When I click the button, negative number comes up. I want it to get to 12-15 but my head is spinning on how to come up with that logic. any help?