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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:35:11+00:00 2026-05-15T01:35:11+00:00

I have an HTML item for a ‘drop down menu’, structured like this… <li

  • 0

I have an HTML item for a ‘drop down menu’, structured like this…

            <li class="ui-dropdown-list" >
                <a href="#">Right Drop Down Menu</a>
                <ul>
                    <li><a href="#">Item</a></li>
                    <li><a href="#">Item</a></li>
                    <li><a href="#">Item</a></li>
                </ul>
            </li>

Using jQuery to make this a dropdown list, with the following code.

jQuery.fn.dropdown = function () {

    var defaults = {
        button: null,
        menu: null,
        visible: false
    };
    var options = $.extend(defaults, options);

    return this.each(function () {
        options.button = $(this);
        options.menu = $(this).find("ul");

        // when the parent is clicked, determine whether dropdown needs to occur
        options.button.click(function () {
            options.visible ? lift(options.menu) : drop(options.menu);
            options.visible = !options.visible;
        });

        // drop the menu down so that it can be seen.
        function drop(e) {
            options.button.addClass("open");
            options.menu.show();
        }
        // lift the menu up, hiding it from view.
        function lift(e) {
            options.menu.hide();
            options.button.removeClass('open');
        }

    });
};

I am trying to wire it up so that if the user clicks anywhere outside of the menu, it will collapse it. This is proving much more difficult than I anticipated; even trying to use page level events. Any suggestions?

The menu itself never really ‘receives’ focus, so using .blur doesn’t seem to be suiting the purpose.

  • 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-15T01:35:12+00:00Added an answer on May 15, 2026 at 1:35 am

    You can use event bubbling to your advantage here, if a click event bubbles all the way to document then close the menus, if it happens in the menu, stop the bubble so that handler on document doesn’t get hit, like this:

    jQuery.fn.dropdown = function () {
        var defaults = {
            button: null,
            menu: null,
            visible: false
        };
        var options = $.extend(defaults, options);
    
        return this.each(function () {
            options.button = $(this);
            options.menu = $(this).find("ul");
            $(document).click(lift); //close on bubble!
    
            options.button.click(function (e) {
                options.menu.is(':visible') ? lift() : drop();
                e.stopPropagation(); //prevent bubble!
            });
    
            function drop(e) {
                options.button.addClass("open");
                options.menu.show();
            }
            function lift(e) {
                options.menu.hide();
                options.button.removeClass('open');
            }
        });
    };
    

    You can see a working demo here 🙂

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

Sidebar

Related Questions

I have HTML that looks like this: <div class=item-list> <h3>Monday Sep 21</h3> <h3>Tuesday Sep
Okay so say I have a form element like this: HTML: <div class=form-item> <label>Current
I have html like this. <span class=gallery-open-item year-icon-Yes 2010> <a href=/year/none> </a> </span> I
I have html structured like so. <div id='container'> <div class='item'> ... </div> <div class='item'>
I have a html table like: <table ..... class=cars> <tr class=item-odd> ... </tr> <tr
I have some HTML that looks like this: <ul class=toggleList> <li><input type=checkbox name=toggleCbx1 id=toggleCbx1
I have following HTML <div id=finalTree> <ul> <li class=last style=display: list-item;> <a id=DataSheets href=#>Data
I have html: <div class=items-list-container> <div class=fieldset-item>Something</div> <div class=fieldset-item>Something 2</div> <div class=fieldset-item>Something 3</div> <div
I have html that looks something like this: <div> <table> <tr onclick=show();> <td class=cell>Click
I have product item webpages located in /product/items/*.html?id=12345&ref=54321 and I would like the webpages

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.