// Array of Stored Records
var dataStore = ['My Data 1','My Data 2','My Data 3','My Data 4','My Data 5','My Data 6','My Data 7','My Data 8','My Data 9','My Data 10','My Data 11','My Data 12','My Data 13','My Data 14','My Data 15','My Data 16','My Data 17','My Data 18','My Data 19','My Data 20'];
var i = 0;
// Next Item in Array
function nextItem() {
i = i + 1; // increase i by one
i = i % dataStore.length; // if we've gone too high, start from `0` again
return dataStore[i]; // give us back the item of where we are now
}
// Previous Item in Array
function prevItem() {
if (i === 0) { // i would become 0
i = dataStore.length; // so put it at the other end of the array
}
i = i - 1; // decrease by one
return dataStore[i]; // give us back the item of where we are now
}
// Event Listener: Page Load
window.addEventListener('load', function () {
// Load Initial to Selector, #output
document.getElementById('output').textContent = dataStore[0]; // initial value
// Event: Previous Button
document.getElementById('prev_button').addEventListener('click',function (e) {
document.getElementById('output').textContent = prevItem();
});
// Event: Next Button
document.getElementById('next_button').addEventListener('click',function (e) {
document.getElementById('output').textContent = nextItem();
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.