How can I add a specific class to an anchor tag with an href equal to the URL using jQuery?


Arman Bagheri

I want a jQuery dynamic menu that navigates to the <a></a>tabs as well as the tabs that come with the page URL.

$('ul.nav.navbar-nav.side-nav.nicescroll-bar li').find('a').each(function() {
  var text = $(this).attr("href");
  if (window.location.href.includes(text)) {
    $('ul.nav.navbar-nav.side-nav.nicescroll-bar li a').addClass('active')
  } else {}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav navbar-nav side-nav nicescroll-bar" style="overflow: hidden; width: auto; height: 100%;">
  <li><a href="home">home</a></li>
  <li><a href="dashboard">dashboard</a></li>
  <li><a href="base">base</a></li>
  <li><a href="test">test</a></li>
</ul>

In this code, all menus have color change, the color of the menu should be changed according to the address of the page.

Bojandra Rania

Just remove the class in else:

if (window.location.href.includes(text)) {
  $(this).addClass('active')
} else {
  $(this).removeClass('active')
}

Related


Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the contents of the specific menu the user was in before the refresh is displayed as active (ie, displayed on

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

How to add class to specific li using anchor tag?

Accor Ltd It's a very simple thing. I know, I've been using this type of thing. Now in one of my projects, the problem I am facing is adding classes. Below is just an HTML example: //html <div> <ul> <li><a href="#waxing" class="st_tab">Waxing</a></

How to add class to specific li using anchor tag?

Accor Ltd It's a very simple thing. I know, I've been using this type of thing. Now in one of my projects, the problem I am facing is adding classes. Below is just an HTML example: //html <div> <ul> <li><a href="#waxing" class="st_tab">Waxing</a></

Can't get anchor tag's href value using jQuery

Sell I'm trying to get the value of an anchor tag hrefusing jQuery , but the thiskeyword is not working as expected. Here is the result of the code in the console: The part of the code I'm having trouble with: $('#container a').click(() => { console.log(th

Can't get anchor tag's href value using jQuery

Sell I'm trying to get the value of an anchor tag hrefusing jQuery , but the thiskeyword is not working as expected. Here is the result of the code in the console: The part of the code I'm having trouble with: $('#container a').click(() => { console.log(th

How to find href path of anchor tag using jquery?

Vignesh Pichamani How can i get and check if href path contains image path or other link using jquery. E.g: <a href="image.png"><img src="image.png"/></a> In the above example, the anchor text contains the image src, then only the jquery function can find the

How to find href path of anchor tag using jquery?

Vignesh Pichamani How can i get and check if href path contains image path or other link using jquery. E.g: <a href="image.png"><img src="image.png"/></a> In the above example, the anchor text contains the image src, then only the jquery function can find the