skip to Main Content
<div class='topnav' id='myTopnav'>
  <div class='dropdown'>
    <button class=' dropbtn'>Adobe ▼</button>
    <div class='dropdown-content'>
        <a href='index'>Photoshop</a>
    </div>
  </div>
</div>
<script>
    jQuery(function($) {
        var path = window.location.href;
        $('#myTopnav a').each(function() {
            if (this.href === path) {
            $(this).addClass('active');
            $(this).parent().parent('first-child').addClass('active');
            }
        });
    });
</script>

I am not very good at jQuery or even Javascript, but I would like to give the button the same class as the anchor tag when the url the anchor is pointing to is correct.

2

Answers


  1. I’ve not working with JQuery for a long time but here is my suggestion.
    You can try this one
    First, find the closet dropdown after that find dropbtn

    $(this).closest('.dropdown').find('.dropbtn').addClass('active');
    

    For finding .dropbtn class as i remember it’s may return all element that have .dropbtn so if you’re in this case I think you may need to get first.
    Hope it’s can help

    Login or Signup to reply.
  2. Select your options and add class.

    $('select#your-select-id option').addClass('your-class-name');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search