Reputation: 1
I had a problem in my code, I need to get the data from API that I have. It has a column I need the data inside it but it have a multiple data, it's likw inside one array and the repeated with different values. I want all these values. I tried to get them but it only giving me the last value. But in console log it returns all values
This is my code:
$.ajax({
url:
'https://tuapi.taibahu.edu.sa/std/v2/course/CS102/namebySymbolNumber',
type: 'GET',
dataType: 'json',
success: function (data) {
for (var i = 0; i < data.length; i++) {
console.log(data[i]);
$(".CourseName .nf-calculation-control").text(data[i].COURSE_NAME);
$("#" + CourseNamejs).val(data[i].COURSE_NAME);
}
},
error: function (error) {
console.log(error);
},
});
Upvotes: 0
Views: 303
Reputation: 23
You are only getting the last one because you are overwriting the previous text as you move through the loop. Thats why you are seeing all of them when you are console logging. Use the jquery append() function to append to an existing tag. http://api.jquery.com/append/
$.ajax({
url:
'https://tuapi.taibahu.edu.sa/std/v2/course/CS102/namebySymbolNumber',
type: 'GET',
dataType: 'json',
success: function (data) {
for (var i = 0; i < data.length; i++) {
console.log(data[i]);
$(".CourseName .nf-calculation-control").append(data[i].COURSE_NAME);
$("#" + CourseNamejs).val(data[i].COURSE_NAME);
}
},
error: function (error) {
console.log(error);
},
});
Upvotes: 0