0

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);
  }
});
2

2 Answers 2

1

I checked your code with this test json

I created this jsfiddle and it seems to work perfect. There must be some kind of error with your json.

Html:

<input id="name" /><br>
<input id="author" /><br>
<input id="content" /><br>
<button id="nextButton">Next</button>

Js:

$(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);

  firstelement = arrayResponse[0];  

     $("#name").val(firstelement.id);
     $("#author").val(firstelement.name);
     $("#content").val(firstelement.username);


  // Pass the posts array to the callback.

};
request.open("GET", "https://jsonplaceholder.typicode.com/users", true);
request.send(null); 



$("#nextButton").bind("click", function(){

    displayItem(arrayResponse[++index])

});     

 function displayItem(item) {
       $("#name").val(item.id);
       $("#author").val(item.name);
       $("#content").val(item.username);        
 }  
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! thanks for youe question .. i replaced url with my localhost (127.0.0.1:8887/posts.json) and WORK IT on jsfiddle.net..so my json file it's good!!. But on the chrome browser doesn't work!!
0

Change this :

$("#nextButton").bind("click", function(){
    displayItem(arrayResponse[++index])
}); 

to this:

$("#nextButton").bind("click", function(){
    displayItem(arrayResponse[index]);
    index++;
}); 

2 Comments

Thanks for your answer.. :) But form re-load first value again
Maybe using jQuery ajax functions is a good idea jQuery get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.