{"id":1658,"date":"2017-06-19T18:04:24","date_gmt":"2017-06-19T12:34:24","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1658"},"modified":"2026-09-07T16:13:13","modified_gmt":"2026-09-07T10:43:13","slug":"make-active-click-menu","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/make-active-click-menu\/","title":{"rendered":"Make the menu to be Active when click menu"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A sidebar menu that doesn&#8217;t visually highlight the current page leaves users guessing where they are in your app. This guide shows how to make a sidebar menu item active on click using jQuery \u2014 automatically applying an active class to the current page&#8217;s link, including support for nested treeview-style submenus like those used in the AdminLTE admin template.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">I. Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before you start, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A web page using jQuery (or willingness to add it)<\/li>\n\n\n\n<li>Basic familiarity with editing HTML and JavaScript<\/li>\n\n\n\n<li>An AdminLTE-based layout, or any sidebar markup you can adapt the selectors to<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">II. Include the Required Libraries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Load jQuery, Bootstrap, and the AdminLTE JS files. Use <code>&lt;script&gt;<\/code> tags \u2014 not <code>&lt;source&gt;<\/code> \u2014 since <code>&lt;source&gt;<\/code> is only valid inside <code>&lt;video&gt;<\/code>, <code>&lt;audio&gt;<\/code>, or <code>&lt;picture&gt;<\/code> elements and will not execute JavaScript:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.7.1\/jquery.min.js\">&lt;\/script>\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/twitter-bootstrap\/3.3.4\/js\/bootstrap.min.js\">&lt;\/script>\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/admin-lte\/2.3.11\/js\/app.min.js\">&lt;\/script>\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> This example uses AdminLTE 2.3.11 to match the original template&#8217;s markup (<code>sidebar-menu<\/code>, <code>treeview-menu<\/code> classes). If you&#8217;re starting a new project, check whether a newer AdminLTE version is a better fit, since the class names and structure can differ between major versions.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">III. Add the Active-Class Script<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This script compares each menu link&#8217;s URL to the current page URL, and adds the <code>active<\/code> class to the matching item \u2014 including a separate check for nested treeview submenus:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$(document).ready(function () {\n  var url = window.location.href;\n\n  \/\/ Highlight top-level sidebar links\n  $('ul.sidebar-menu a').filter(function () {\n    return this.href === url;\n  }).parent().addClass('active');\n\n  \/\/ Highlight parent treeview when a nested link matches\n  $('ul.treeview-menu a').filter(function () {\n    return this.href === url;\n  }).closest('.treeview').addClass('active');\n});\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Wrapping the logic in <code>$(document).ready(...)<\/code> ensures it only runs once the full page has loaded, so it doesn&#8217;t try to match links before they exist in the DOM.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">IV. Build the Sidebar Menu HTML<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a working sidebar structure. Each <code>&lt;a&gt;<\/code> tag&#8217;s <code>href<\/code> should point to a real page on your site \u2014 when that page loads, the script above will detect the match and apply the <code>active<\/code> class automatically:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n  &lt;meta charset=\"UTF-8\">\n  &lt;title>Admin Dashboard&lt;\/title>\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">\n\n  &lt;!-- Bootstrap 3.3.4 -->\n  &lt;link href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/twitter-bootstrap\/3.3.4\/css\/bootstrap.css\" rel=\"stylesheet\" type=\"text\/css\">\n  &lt;!-- FontAwesome 4.7.0 -->\n  &lt;link href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/font-awesome\/4.7.0\/css\/font-awesome.css\" rel=\"stylesheet\" type=\"text\/css\">\n  &lt;!-- Ionicons 2.0.1 -->\n  &lt;link href=\"https:\/\/code.ionicframework.com\/ionicons\/2.0.1\/css\/ionicons.min.css\" rel=\"stylesheet\" type=\"text\/css\">\n  &lt;!-- AdminLTE Theme -->\n  &lt;link href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/admin-lte\/2.3.11\/css\/AdminLTE.min.css\" rel=\"stylesheet\" type=\"text\/css\">\n  &lt;!-- AdminLTE Skin -->\n  &lt;link href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/admin-lte\/2.3.11\/css\/skins\/skin-blue.css\" rel=\"stylesheet\" type=\"text\/css\">\n&lt;\/head>\n&lt;body class=\"sidebar-mini skin-blue\">\n\n&lt;aside class=\"main-sidebar\">\n  &lt;section class=\"sidebar\">\n    &lt;ul class=\"sidebar-menu\">\n      &lt;li class=\"header\">MAIN NAVIGATION&lt;\/li>\n\n      &lt;li class=\"treeview\">\n        &lt;a href=\"#\">\n          &lt;i class=\"fa fa-dashboard\">&lt;\/i> &lt;span>Dashboard&lt;\/span>\n          &lt;i class=\"fa fa-angle-left pull-right\">&lt;\/i>\n        &lt;\/a>\n        &lt;ul class=\"treeview-menu\">\n          &lt;li>&lt;a href=\"index.html\">&lt;i class=\"fa fa-circle-o\">&lt;\/i> Dashboard v1&lt;\/a>&lt;\/li>\n          &lt;li>&lt;a href=\"index2.html\">&lt;i class=\"fa fa-circle-o\">&lt;\/i> Dashboard v2&lt;\/a>&lt;\/li>\n        &lt;\/ul>\n      &lt;\/li>\n\n      &lt;li class=\"treeview\">\n        &lt;a href=\"#\">\n          &lt;i class=\"fa fa-files-o\">&lt;\/i> &lt;span>Layout Options&lt;\/span>\n          &lt;span class=\"label label-primary pull-right\">4&lt;\/span>\n        &lt;\/a>\n        &lt;ul class=\"treeview-menu\">\n          &lt;li>&lt;a href=\"pages\/layout\/top-nav.html\">&lt;i class=\"fa fa-circle-o\">&lt;\/i> Top Navigation&lt;\/a>&lt;\/li>\n          &lt;li>&lt;a href=\"pages\/layout\/boxed.html\">&lt;i class=\"fa fa-circle-o\">&lt;\/i> Boxed&lt;\/a>&lt;\/li>\n          &lt;li>&lt;a href=\"pages\/layout\/fixed.html\">&lt;i class=\"fa fa-circle-o\">&lt;\/i> Fixed&lt;\/a>&lt;\/li>\n        &lt;\/ul>\n      &lt;\/li>\n\n      &lt;li>\n        &lt;a href=\"pages\/widgets.html\">\n          &lt;i class=\"fa fa-th\">&lt;\/i> &lt;span>Widgets&lt;\/span>\n          &lt;small class=\"label pull-right bg-green\">new&lt;\/small>\n        &lt;\/a>\n      &lt;\/li>\n\n    &lt;\/ul>\n  &lt;\/section>\n&lt;\/aside>\n\n&lt;div class=\"container\" style=\"padding:10px\">\n  &lt;!-- Page content goes here -->\n&lt;\/div>\n\n&lt;!-- Scripts -->\n&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.7.1\/jquery.min.js\">&lt;\/script>\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/twitter-bootstrap\/3.3.4\/js\/bootstrap.min.js\">&lt;\/script>\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/admin-lte\/2.3.11\/js\/app.min.js\">&lt;\/script>\n\n&lt;script>\n  $(document).ready(function () {\n    var url = window.location.href;\n\n    $('ul.sidebar-menu a').filter(function () {\n      return this.href === url;\n    }).parent().addClass('active');\n\n    $('ul.treeview-menu a').filter(function () {\n      return this.href === url;\n    }).closest('.treeview').addClass('active');\n  });\n&lt;\/script>\n\n&lt;\/body>\n&lt;\/html>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">V. What Was Fixed From the Original Markup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A few issues in a typical first draft of this code would stop it from working correctly:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>&lt;source><\/code> tags don&#8217;t run JavaScript.<\/strong> Every script include (jQuery, Bootstrap, AdminLTE, and the active-menu logic itself) needs to be inside a <code>&lt;script><\/code> tag, not <code>&lt;source><\/code> \u2014 browsers simply ignore JS placed in a <code>&lt;source><\/code> element.<\/li>\n\n\n\n<li><strong>Duplicate <code>&lt;head><\/code> tags break the document structure.<\/strong> A page should only have one <code>&lt;head><\/code> opening and closing tag; nesting a second <code>&lt;head><\/code> inside the first produces invalid, unpredictable HTML.<\/li>\n\n\n\n<li><strong>jQuery was being loaded twice<\/strong> \u2014 once in the (invalid) head section and again near the bottom. Include it exactly once, ideally right before your closing <code>&lt;\/body><\/code> tag for faster initial page rendering.<\/li>\n\n\n\n<li><strong>Loose equality (<code>==<\/code>) was used to compare <code>checked<\/code>\/URL values<\/strong> in similar scripts \u2014 using strict equality (<code>===<\/code>) avoids unexpected type-coercion bugs and is generally better practice in JavaScript.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">VI. Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Highlighting the active sidebar menu item comes down to comparing each link&#8217;s <code>href<\/code> against the current page URL with jQuery, then applying an <code>active<\/code> class to the matching item \u2014 with a second check to also activate any parent treeview when a nested link matches. Fixing the <code>&lt;source&gt;<\/code>-instead-of-<code>&lt;script&gt;<\/code> mistake and removing the duplicate <code>&lt;head&gt;<\/code> tag are what actually make this example run; without those two corrections, the script never executes at all.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why does the treeview parent need a separate check?<\/strong> Because clicking a nested link only matches that specific <code>&lt;a&gt;<\/code> tag \u2014 without the second filter targeting <code>.treeview-menu a<\/code>, the parent dropdown itself would never get the <code>active<\/code> class, even though one of its child links matches the current page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Does this work with single-page applications (SPAs) that don&#8217;t do full page reloads?<\/strong> Not reliably. This approach checks <code>window.location.href<\/code> on page load, so it works well for traditional multi-page sites. For SPAs using client-side routing, you&#8217;d typically hook into the router&#8217;s navigation events instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Can I use this same technique outside of AdminLTE?<\/strong> Yes \u2014 the core logic (comparing <code>this.href<\/code> to the current URL and adding a class) works with any sidebar markup. You&#8217;d just adjust the jQuery selectors (<code>ul.sidebar-menu<\/code>, <code>ul.treeview-menu<\/code>) to match your own menu&#8217;s class names.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Technology Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Building out an admin dashboard or improving your site&#8217;s front end? Our team can help with front-end development and custom web application builds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/contact\">Connect with our technology experts.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/check-uncheck-checkbox-using-jquery\/\">Check\/Uncheck a Checkbox Using jQuery<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A sidebar menu that doesn&#8217;t visually highlight the current page leaves users guessing where they are in your app. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[341,350,273],"class_list":["post-1658","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter","tag-javascriptjquery","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-qK","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1658"}],"version-history":[{"count":3,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":11449,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1658\/revisions\/11449"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}