OiO.lk Blog jQuery I need to give an element the same class as another element
jQuery

I need to give an element the same class as another element


<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.



You need to sign in to view this answers

Exit mobile version