13
I’m bringing back notifications and need to dynamically add the path to them, how do I add a <li>
to each element of the response?
Javascript
$(document).ready(function () {
$.ajax({
url: "/tasks/not-assign",
dataType: "JSON",
success: function (res) {
for(var i = 0; i<res.length; i++) {
$('#taskItem').html(res[i].title);
var a = document.getElementById('taskItem'); //or grab it by tagname etc
a.href = "/tasks/detail/" + res[i].id;
}
}
});
});
HTML
<li>
<a href="#" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false" style="color: #fff"><i class="fa fa-bell-o" aria-hidden="true"></i><span id="numberTask" class="badge"></span></a>
<ul class="dropdown-menu">
<li><a id="taskItem"></a></li>
</ul>
</li>
As it stands it brings only the last item of Collection, I need to add the <li>
dynamically.
Excellent use of the
$.each
– Caio Felipe Pereira