Good day!! My issue is this...
I sent an ajax request to get some details at the backend. After splitting the response i have 2 different arrays set to a variable.
I.e **allQst** [1,2,3,4,5,6] and **attempted** [1,2,3,4].
I now looped allQst with foreach and created an li element to list each at the frontend which works fine. The issue now is trying to add a different id for each li that would be listed.
I tried to add .attr() to the li as in,
<li>bla bla...</li>.html(item).attr("id","listing"+item);
This gives it the same id. I.e listing1 for all li
I also tried to make it
<li>bla bla...</li>.html(item).attr('id', function(i) { return 'listing'+(i+1); });
Doesn't work.
This is my code in full...
$(document).ready(function() {
$("#next,#prev").one('click', function() {
$.ajax({
url: 'done.php',
method: 'post',
dataType: "JSON",
success: function(response) {
var rsp = response.r2;
var rspB = response.r1;
var allQst = rsp.split(",");
var attempted = rspB.split(",");
attempted.forEach(func2);
allQst.forEach(func);
function func(item, index) {
var ul = $("#list");
var li = $('<li class="pagination-link"></li>').html(item).attr('id', function(i) {
return 'listing' + (i + 1);
});
ul.append(li);
}
function func2(item, index) {
$("#" + item).addClass("is-current");
}
} //succes func end
}); //ajax end
}); //click func end
}); //doc end
What I want to achieve lastly is to get the id and add a class to each of them.
I would be glad if you can assist me!! Thanks in advance.