I have and array of data by ajax get method. I output this data by its index number as data[0]. for next data I click on a button and need to update the index “here 0” in data to get data[1] and so on. Here is my code, I have tested with defining id after .done but doesn’t work.
$.ajax({
url: `/ajaxweblog`,
id: 0,
}).done(function (data) {
$("#rast").on('click', function () {
this.id += 1;
});
$(".title-of").html(data[this.id]['title']);
$(".subtitle-of").html(data[this.id]['subtitle']);
$(".thearticel").html(data[this.id]['body']);
$(".article-img").attr('src', '/images/articles/' + data[this.id]['image']);
})
2
Answers
Firstly remove click handle code that you inserted inside ‘.done’
$("#rast").on('click', function () {
this.id += 1;
});
then you have to keep a copy of response object and on click of a button (or change in the id), you have to pass updated JSON data for table to render