Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
cant remove class from menu item

I have a dropdown menu. On some pages, the link just scrolls to the anchor instead of refreshing the page. When this happens then dropdown menu is supposed to close. But it does not.

This is my html

<div class="navbar">
    <div class="nav_item">
        <div class="dropdown menu_item" >
            <span class="navbar-submenu"> <a href="/">dropdown</a></span>
            <div class="secondarynav" >
                <div class="nav_item">
                    <div class="menu_item" >
                        <a href="/#">link</a>
                    </div>
                </div>
                <div class="nav_item">
                    <div class="menu_item" >
                        <a href="/#">link</a>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <div class="nav_item">
        <div class="menu_item" >
            <a href="/">link</a>
        </div>
    </div>
</div>

What is supposed to happen is when a visitor clicks on dropdown, active class is added to the parent nav_item, which then shows the rest of the menu. When a user clicks dropdown again active is removed and dropdown closes.

$('.dropdown').click(function (e) {
    $(this).closest('.nav_item').toggleClass('active');
});

Now the problem, when a visitor clicks dropdown, then any of the children links I would like the active class removed, which currently only happens when the dropdown link is clicked.

I have tried various other methods

$('.nav_item.active > *').click(function(e) {
    $(this).closest('.active').removeClass('active');
});

But this glitches with the toggle function in the other scipt. Basically the active is added then removed straight away.

How can I set it so when .dropdown is clicked .active is added to the parent nav_item. But then if any link in that menu is clicked the .active is removed? Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use e.stopPropagation() to prevent the event on the item from bubbling up to the dropdown.

$('.nav_item.active > *').click(function(e) {
    e.stopPropagation();
    $(this).closest('.active').removeClass('active');
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!