skip to Main Content

AdminLte Active Current Menu Using JAVASCRIPT

AdminLte active current menu using JAVASCRIPT


AdminLte is the most popular and open source admin theme. many developers used AdminLte for developing admin panel. Many Developers use PHP if a condition for active the current sidebar menu. It’s hard to manage with if condition when we have many sidebar menus.

I think we should use JAVASCRIPT for active the current menu for the sidebar. it’s too easy and simple js snippets.

$(document).ready(function(){

    var url = window.location;
    // Will only work if string in href matches with location
    $('ul.sidebar-menu a[href="'+ url +'"]').parent().addClass('active');

    // Will also work for relative and absolute hrefs
    $('ul.sidebar-menu a').filter(function() {
        return this.href == url;
    }).parent().addClass('active');

    // Will also work for relative and absolute hrefs
    $('ul.sidebar-menu a').filter(function() {
        var regex = new RegExp('\\b' + this.href + '\\b');
        return url.href.search(regex) !== -1;
    }).parent().addClass('active');

});

Just change the UL sidebar-menu Class before use this code.

Just add code to your javascript file. then checked the “active” class is added to your current menu.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top