I have AJAX call
Here is code
$.ajax({
url: '@Url.Action("EmailsList", "Questions")',
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json',
processData: false,
success: function (result) {
var email = result;
// console.log(result[0].Name);
for (var i = 0; i <= email.length - 1; i++) {
var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
'<b style="margin-left: 10px;">' + result.indexOf(this)+
result[i].Email +
'<b>' +
'<b style="margin-left: 20px;">' +
result[i].Name +
'</b>' +
'</div>';
$(".email_list").append(emailHTML);
}
}
});
}
I need to write element number from 1, I tried this result.indexOf(this), but it not works
How I can do this?
You can simply use the variable i with which you are iterating like so:
var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
'<b style="margin-left: 10px;">' +(i+1)+
result[i].Email +
'<b>' +
'<b style="margin-left: 20px;">' +
result[i].Name +
'</b>' +
'</div>';
$(".email_list").append(emailHTML);
The specific reason for using (i+1) is because i starts its iteration from 0 and you want it from 1.
If I understood, you should write something like :
var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
'<b style="margin-left: 10px;">' + (i + 1) +
result[i].Email +
'<b>' +
'<b style="margin-left: 20px;">' +
result[i].Name +
'</b>' +
'</div>';
Just use the value of your variable i incremented in loop.