Code for automatic next/prev buttons

The following code can be used to automatically create links for next/prev buttons, so that you don’t need to define them manually.

I don’t have a video Tutorial for this yet. So if you are interested in this function, let me know, and I will show you how to integrate it into your website.

For now this is just a reminder for myself, so that I can easily find this code again 😉

<script>
jQuery(function($){
	var links = [
		"https://example.com/project1/",
		"https://example.com/project2/",
		"https://example.com/project3/"
	];
	
	var currentLink = window.location.pathname;
	var currentIndex = false;
	var nextIndex;
	var prevIndex;
	for (var i = 0; i < links.length; i++) {
    	if(links[i].indexOf(currentLink) > -1){
			currentIndex = i;
			if(currentIndex == 0){
				nextIndex = 1;
				prevIndex = links.length-1;
			}else if(currentIndex == links.length-1){
				nextIndex = 0;
				prevIndex = links.length-2;
			}else{
				nextIndex = currentIndex+1;
				prevIndex = currentIndex-1;
			}
		}
    };
	$(".nextButton, .prevButton").css("cursor","pointer");
	$(".nextButton").click(function(){
		if(currentIndex){
			window.open(links[nextIndex],"_self");	
		}
	});
	$(".prevButton").click(function(){
		if(currentIndex){
			window.open(links[prevIndex],"_self");	
		}
	});
});
</script>