I am trying to display next item from array json with displayItem function, but after click on the button, the form reloads the first value (arrayResponse[0]). What should I do to display the next item instead?
$(document).ready(function () {
var firstElement;
var arrayResponse;
var index = 0;
var request = new XMLHttpRequest();
// When the file has loaded,
request.onload = function () {
// parse the JSON text into an array of post objects.
arrayResponse = JSON.parse(request.responseText);
var firstelement = arrayResponse[0];
$("#name").val(firstelement.name);
$("#author").val(firstelement.author);
$("#content").val(firstelement.content);
// Pass the posts array to the callback.
};
request.open("GET", "http://127.0.0.1:8887/posts.json", true);
request.send(null);
$("#nextButton").bind("click", function () {
displayItem(arrayResponse[++index])
});
function displayItem(item) {
$("#name").val(item.name);
$("#author").val(item.author);
$("#content").val(item.content);
}
});