Here's the scenario: I have a menubar.js script, which, when the page loads, queries the server for user's permissions, and then creates a menubar based on them. code snippet:
$(function () {
//initialize the header buttons according to user's permissions
InitializeCurrentUser();
});
function InitializeCurrentUser() {
//get current user
$.ajax(
{
url: "../Login/LoginService.asmx/GetCurrentUser",
success: function (result) {
var currentUserRoles = result.d;
output = '<ul class="Header"> ';
if ($.inArray("reports", currentUserRoles) > -1) {
output += '<a href="../Reports/Reports.aspx" ><li id="lnkReports">Reports </li></a>';
}
//etc...
console.log('generating menu');
$("#HeaderMenu").html(output);
}
});
}
This script is included in all my pages.
Now, i'd like to mark the <li>
representing the current page with a 'current' class.
I've added var currentPage
to my menubar.js script, and on each page (for example, in reportsPage.js):
$(function() {
//set this page as the current page;
currentPage = Enum.Page.Reports;
});
and in menubar.js: a function which is called on document.ready:
function SetCurrentPage() {
$("#HeaderMenu li").removeClass('current');
var liElement = null;
switch (currentPage) {
case Enum.Page.Reports:
liElement = $("#lnkReports");
break;
}
console.log('adding a class to: ');
console.log(liElement);
liElement.addClass('current');
}
the log that i get is:
> generating menu
> adding a class to:
> []
so it appears that the element i'm looking for hasn't been added to the DOM yet.
I think there should be some solution using live()
, but when I try live('load',...)
it never gets called...