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

  • Home
  • SEARCH
  • 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 8107167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:40:49+00:00 2026-06-06T00:40:49+00:00

Cant get this script to work with my menu really have tried everything any

  • 0

Cant get this script to work with my menu really have tried everything any ideas? The divs has a background image which is on hover then jquery does the on click release li menu… is the hover function messing it up?

<div class="top_menu">
    <div class="top_menu_menub">
        <ul id="menu">
            <li class="sub">
                <ul>
                    <li><a href="">Control Center</a></li>
                    <li><a href="">APEC Trinity</a></li>
                    <li><a href="">APEC Living</a></li>
                    <li><a href="">APEC Energy</a></li>
                </ul>
            </li>
        </ul>
    </div>
</div>​

jQuery:

$(function() {
    $(document).ready(function() {
        $('li').click(function(e) {
            var $this = $(this);
            var ulId = $this.attr("href");
            var clicked_menu_is_visible = $this.parent().find("ul" + ulId).filter(':visible').length > 0;
            var visible_uls = $this.parent().find("ul").filter(':visible');
            if (visible_uls.length === 0) { //no menus showing - just show clicked menu 
                $ul = $this.parent().find("ul" + ulId);
                $ul.slideToggle('medium');
            }
            else { //close open menus (should only be one open) then open clicked menu
                //via callback 
                $this.parent().find("ul").filter(':visible').slideUp("medium", function() {
                    $ul = $this.parent().find("ul" + ulId);
                    //open clicked menu - unless menu was already open when clicked
                    if (!clicked_menu_is_visible) {
                        $ul.slideToggle('medium');
                    }
                });
            }
            console.log($(this).children()[0].innerText);
            e.preventDefault();
            return false;
        });
    });
});​

CSS:

.top_menu_menub {
    padding: 3px 0px 0px 0px;
    width: 200px;
    float: left;
    height: 20px;
    text-align: left;
    font-family: Helvetica, sans-serif;
    font-size: 10px;
    font-weight: bold;
    color: #aaa;
    background-image: url(../images/apecbuttona.jpg);
    background-repeat: no-repeat;
    background-position: 8px 4px;
    cursor: pointer;
}
#menu {
    list-style-type: none;
    width: 200px;
    padding: 0;
    margin: 0 auto;
    height: 24px;
}
#menu ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    border: solid 1px#eee;
    border-radius: 5px;
}
#menu li {
    float: left;
    margin: 1px 1px 0 0;
    position: relative;
    z-index: 9999
}
#menu li.sub {
    width: 200px;
    height: 16px;
    padding: 1px 0px 0px 0px;
}
#menu li.sub:hover {
    color: #00CCFF;
    background-image: url(../images/apecbutton.jpg);
    background-repeat: no-repeat;
    background-position: 8px 0px;
    height: 24px;
}
#menu li a {
    display: block;
    color: #999;
    font-family:arial, sans-serif;
    font-size:11px;
    line-height:23px;
    width:107px;
    text-decoration:none;
    text-align:left;
    cursor:pointer;
    font-weight:100;
    border-bottom: 1px solid # eee;
    padding-left: 8px;
}
#menu li a:hover {
    background: #fff;
    color: #4FA4F9;
}
#menu ul {
    position: absolute;
    left: -9800px;
    width: 115px;
}
#menu li.click {
}
#menu li.click ul {
    left: 12px;
    top: 22px;
    background: #fff;
}
/* the background image is for IE7 */

​
  • 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-06T00:40:51+00:00Added an answer on June 6, 2026 at 12:40 am

    Your click handler is capturing the click event for <li class="sub">. I don’t know if that’s what you want, but it has the side-effect of ensuring $this.attr("href") consistently returning undefined as the li element lacks that attribute.

    This leads to the problem of var ulId being undefined, which in turn leads to the problem of $ul always being an empty jQuery object, so nothing else happens.

    Also, your CSS leads to everything being initially not visible or parked 9800px offscreen to the left. Did you mean for something to be visible initially so the user knows where to click or to hover?

    Please clarify in your question the behavior you are expecting.

    UPDATE:

    Per the clarification, below is your jQuery code modified to do what you want:

    $(function() {
        var toggleMenu = function(e) {
            var $this = $(this),
                ulId = $this.attr("href") || $this.find('a').attr("href"),
                $ul = $this.parents("ul").filter("ul#" + ulId), //cache query
                $all = $("ul#menu ul"), //cache query
                clicked_menu_is_visible = $ul.is(':visible'), //changed .filter to .is
                visible_uls = $all.is(':visible'); //changed .filter to .is
            if (!visible_uls) { //no menus showing - just show clicked menu 
                $ul.slideToggle('medium');
            }
            else { //close open menus (should only be one open) then open clicked menu via callback
                $all.filter(':visible').slideUp("medium", function() {
                    //open clicked menu - unless menu was already open when clicked
                    if (!clicked_menu_is_visible) {
                        $ul.slideToggle('medium');
                    }
                });
            }
            console.log('hideMenu triggered');
            e.preventDefault();
            return false;
        };
        $(document).ready(function() {
            $('div.top_menu_menub').click(function(e) {
                $('ul#menu ul').show('medium');
                console.log('div clicked');
                e.preventDefault();
                return false;
            });
            $('li.sub li, ul#menu a').click(toggleMenu);
            $('ul#menu ul').mouseleave(toggleMenu);
        });
    });​
    

    This works with your HTML structure and CSS. Working fiddle here.

    As a side note, I would write it instead as the following:

    $(function() {
        var toggleMenu = function(e) {
            var self = $(this),
                elemType = self[0].tagName.toLowerCase(),
                //get caller
                menu = null;
            if (elemType === 'a') {
                //anchor clicked, nav back to containing ul
                menu = self.parents('ul').not('ul#menu');
            } else if (elemType === 'ul') {
                //mouseleft ul, ergo menu is this.
                menu = self;
            }
            if (menu) {
                menu.hide('medium');
            }
            e.preventDefault();
            return false;
        };
        $(document).ready(function() {
            $('div.top_menu_menub').click(function(e) {
                $('ul#menu ul').show('medium');
                console.log('div clicked');
                e.preventDefault();
                return false;
            });
            $('ul#menu a').click(toggleMenu);
            $('ul#menu ul').mouseleave(toggleMenu);
        });
    });​
    

    This also works with your HTML structure. Working fiddle here.

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

Sidebar

Related Questions

I have been trying everything I can to get this script to work and
I cant get this script to work, $users should hold the array data we
I swear this script worked fine last night but can't get it to work
I'm probably doing something realy stupid but I cant get this to work: var
I'm new to javascript and cant get this little thing to work. (all external
I cant get this to work. I am trying to replace certain characters when
This is probably very easy but I cant get it to work as I'd
I have tried to wrap this in a function and cannot get it to
For some reason I can't get this script to run when I add the
Revising for php and cant seem to get this to print the values out

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.