Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3402716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:10:20+00:00 2026-05-18T05:10:20+00:00

I have a div I want to hide when clicking outside it. It almost

  • 0

I have a div I want to hide when clicking outside it.

It almost works, except the button class is only changing once every two times.

I click on the button, the active button class is added and the dropdown slides down, I click somewhere outside the dropdown, it slides up and the button class returns to it’s original state. BUT, when I repeat this, the button class ‘isActive’ is not added.

Here’s the function:

function toggleSelectGroupList(){
    $('#groupListSortby').slideToggle(30);


    $('body').click(function(){  
        $('#groupListSortby').click(function(e) {    e.stopPropagation();   })
        $('#groupListSortby').hide(); 
        $('.sortFriends .btngrey .gfx').toggleClass('isActive');
        $('.sortFriends .btngrey a').toggleClass('isActive');
    }); 
}

Markup:

<div class="sortFriends">

<div class="btngrey">
    <span class="gfx"></span>
    <a onClick="toggleSelectGroupList()">All friends</a>
</div>

<div class="dropdownList" id="groupListSortby">
    <ul>
        <li class="isActive">
            <span class="gfx"></span>
            <a href="#">All friends</a>
        </li>
        <li>
            <a href="#">Recently added</a>
        </li>
        <li class="last">
            <a href="#">The Railers</a>
        </li>
    </ul>
</div>

</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:10:20+00:00Added an answer on May 18, 2026 at 5:10 am

    You should bind your click handlers from the beginning, and the propagation stop should be immediately bound as well, like this overall:

    $(function() {
      $(".sortFriends .btngrey a").click(function(e) {
        $('#groupListSortby').slideToggle(30);
        $('.sortFriends .btngrey .gfx, .sortFriends .btngrey a').toggleClass('isActive');
        e.stopPropagation();
      });
      $('#groupListSortby').click(function(e) {
        e.stopPropagation();   
      });
      $('body').click(function(){  
        $('#groupListSortby').hide(); 
        $('.sortFriends .btngrey .gfx, .sortFriends .btngrey a').removeClass('isActive');
      }); 
    });
    

    To match, your <a> shouldn’t have an onclick inline with this anymore, just this will do:

    <div class="btngrey">
        <span class="gfx"></span>
        <a href="#">All friends</a>
    </div>
    

    You can test it out here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div I want to hide when I press F2... Everything works
I have this div: <div class=inauguration-image> I do not want this text to display,
I have a list with two <div> s in every <li> and I want
I display the div by clicking the link, but now I want to hide
I have done a hide and show of a div element, on clicking a
I have this code to show/hide (toggle) a div when clicking a link: $('#complete-services-link').toggle(
Right now I can show a div by clicking a button, and hide it
I have a div that I want to appear based on user input (works),
I have an asp.net page, and I want to hide a div on the
I'm want to have a div with class a to be invisible. I've tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.