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 9021339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:13:56+00:00 2026-06-16T05:13:56+00:00

I have a <ul> that when clicked, toggles the visibility of another <ul> .

  • 0

I have a <ul> that when clicked, toggles the visibility of another <ul>. How can I attach an event to the body of the page when the <ul>s are revealed so that the body will hide the <ul>.

I am new to writing these sorts things which bubble, and I cannot figure out why what I have done so far seems to work intermittently. When clicked several times, it fails to add the class open when the secondary <ul> is opened.

And of course, there may be an entirely better way to do this.

$(document).on('click', '.dd_deploy', function (e) {
    var ul = $(this).children('ul');
    var height = ul.css('height');
    var width = ul.css('width');
    ul.css('top', "-" + height);
    ul.fadeToggle(50, function () {

        //add open class depending on what's toggled to
        if (ul.hasClass('open')) {
            ul.removeClass('open');
        } else {
            ul.addClass('open');
        }
        //attach click event to the body to hide the ul when
        //body is clickd
        $(document).on('click.ddClick', ('*'), function (e) {
            e.stopPropagation();
           //if (ul.hasClass('open')) {
                ul.hide();
                ul.removeClass('open')
                $(document).off('click.ddClick');
           // }
        });
    });
});​

http://jsfiddle.net/JYVwR/

  • 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-06-16T05:13:57+00:00Added an answer on June 16, 2026 at 5:13 am

    I’d suggest not binding a click event in a click event, even if you are unbinding it. Instead, i would do it this way:

    http://jsfiddle.net/JYVwR/2/

    $(document).on('click', function (e) {
        if ( $(e.target).is(".dd_deploy") ) {
            var ul = $(e.target).children('ul');
            var height = ul.css('height');
            var width = ul.css('width');
            ul.css('top', "-" + height);
            ul.fadeToggle(50, function () {
    
                //add open class depending on what's toggled to
                if (ul.hasClass('open')) {
                    ul.removeClass('open');
                } else {
                    ul.addClass('open');
                }
            });
        }
        else {
            $('.dd_deploy').children('ul:visible').fadeOut(50,function(){
                $(this).removeClass("open");
            })
        }
    });​
    

    If you need to further prevent clicking on the opened menu from closing the menu, add an else if that tests for children of that menu.

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

Sidebar

Related Questions

I have a div that if clicked toggles another div further down the page.
I have a button in my XML, that when clicked will toggle the visibility
I have a simple jQuery function that toggles a div's visibility when clicked. The
So I have an element that toggles between two background colours when clicked. But
I have a button that when clicked will run a stored procedure on a
I have a UIImageView object that when clicked it will play a animation, I
I have some JavaScript that toggles the class of the th element clicked to
I have a fairly mundane piece jQuery that toggles a div's visibility when a
I have some divs on my page that when a link is clicked they
I have a form with two radio buttons, Yes and No, that toggles visibility

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.