I’m trying to populate a <span></span> element on the page load with jQuery.
At the moment the value that gets populated into the span is just an integer count.
Here I have named my span userCount:
<a href="#" class="">Users<span id = "userCount"></span></a>
I am trying to write the value of the span with no success.
$(document).ready(function () {
$.post("Dashboard/UsersGet", {}, function (dataset) {
var obj = jQuery.parseJSON(dataSet);
var table = obj.Table;
var countUsers;
for (var i = 0, len = table.length; i < len; i++) {
var array = table[i];
if (array.Active == 1) {
var name = array.Name;
}
countUsers = i;
}
userCount.innerHTML = countUsers.toString();
});
});
You don’t have any
usercountvariable. Use$(selector)to build a jquery object on which you can call functions like html.Note also that
countUserswill always betable.length-1.dataSetinstead ofdataset. Javascript is case sensitive.jQuery.postchecks the type of the provided parametersSo, this is probably more what you need, supposing you do other things in the loop :