I am trying to dynamically add active to menu using JavaScript but something i am doing it wrong so it is not happening.
my menu html code is like this
<aside class="sidenav-main nav-expanded nav-lock nav-collapsible sidenav-light navbar-full sidenav-active-rounded">
<div class="brand-sidebar">
<h1 class="logo-wrapper"><a class="brand-logo darken-1" href="index.html"><img src="../app-assets/images/logo/materialize-logo.png" alt="materialize logo"/><span class="logo-text hide-on-med-and-down">SAMIL</span></a><a class="navbar-toggler" href="#"><i class="material-icons">radio_button_checked</i></a></h1>
</div>
<ul class="sidenav sidenav-collapsible leftside-navigation collapsible sidenav-fixed menu-shadow" id="slide-out" data-menu="menu-navigation" data-collapsible="menu-accordion">
<li class="bold"><a class="collapsible-header waves-effect waves-cyan " href="JavaScript:void(0)"><i class="material-icons">settings_input_svideo</i><span class="menu-title" data-i18n="Dashboard">Dashboard</span></a>
<div class="collapsible-body">
<ul class="collapsible collapsible-sub" data-collapsible="accordion">
<li><a href="dashboard.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Analytics">Dashboard</span></a>
</li>
<li><a href="searchDashboard.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Analytics">Search Dashboard</span></a>
</li>
<li><a href="userDashboard.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Analytics">User Dashboard</span></a>
</li>
<li><a href="hitDashboard.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Analytics">Hit Dashboard</span></a>
</li>
</ul>
</div>
</li>
<li class="navigation-header"><a class="navigation-header-text">Users </a><i class="navigation-header-icon material-icons">more_horiz</i>
</li>
<li class="active bold"><a class="collapsible-header waves-effect waves-cyan " href="JavaScript:void(0)"><i class="material-icons">face</i><span class="menu-title" data-i18n="User">User</span><span class="badge badge pill purple float-right mr-10">3</span></a>
<div class="collapsible-body">
<ul class="collapsible collapsible-sub" data-collapsible="accordion">
<li class="active"><a class="active" href="clientList.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="List">Client List</span></a>
</li>
<li><a href="userList.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="View">User List</span></a>
</li>
</ul>
</div>
</li>
<li class="navigation-header"><a class="navigation-header-text">Prediction </a><i class="navigation-header-icon material-icons">more_horiz</i>
</li>
<li class="bold"><a class="collapsible-header waves-effect waves-cyan " href="JavaScript:void(0)"><i class="material-icons">content_paste</i><span class="menu-title" data-i18n="Pages">Prediction</span></a>
<div class="collapsible-body">
<ul class="collapsible collapsible-sub" data-collapsible="accordion">
<li><a href="predictPrice.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Contact">Price Prediction</span></a>
</li>
<li><a href="vDataCount.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="View">Top Buyer</span></a>
</li>
</ul>
</div>
</li>
</ul>
<div class="navigation-background"></div><a class="sidenav-trigger btn-sidenav-toggle btn-floating btn-medium waves-effect waves-light hide-on-large-only" href="#" data-target="slide-out"><i class="material-icons">menu</i></a>
</aside>
I have to add active under the ul when page is active just like below code.
<li class="active"><a href="dashboard.php"><i class="material-icons">radio_button_unchecked</i><span data-i18n="Analytics">Dashboard</span></a>
I am trying using JavaScript but not getting successfully. can some help me how to do it correctly using JavaScript or jQuery.
You have forward slash with pathname, so you have to remove it.
<script>
var pathname = window.location.pathname;
var pathname = pathname.substring(pathname.lastIndexOf('/') + 1);
$('.collapsible > li > a[href="'+pathname+'"]').parent().addClass('active');
</script>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.