Michael
Michael

Reputation: 5

drop down menu toggle does not work

So I am trying to create a dropdown menu using jQuery and I have tried several things. I tried using .hover(), .click(), and mouseenter() for the user's action, but nothing seems to work. I even tried toggle() and slideDown() for the event but nothing shows up on the screen when I hover over or click "Galleries". I know that jQuery is working though because .hide() is working. Here is the html and js

<html>
<head>
    <link type="text/stylesheets" rel="stylesheet" href="ceb.css">
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="background">
        <div id="menu">
            <ul>
                <li><a href=#"">Home</a></li>
                <li><a href="#">Investments</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
                <li><a class"drop-menu" href="#" >Galleries</a>
                <ul id="dropdown">
                    <li><a href="#">Weddings</a></li>
                    <li><a href="#">Engagements</a></li>
                    <li><a href="#">Birthdays</a></li>
                    <li><a href="#">Family</a></li>
                </ul>
                </li>
            </ul>
        </div>
    </div>
<script type="text/javascript">
    var main = function() {
        $('#dropdown').hide();

        $('.drop-menu').mouseenter(function() {
            $('#dropdown').slideDown();
        });
    };

    $(document).ready(main);
</script>
</body>

Here is the css

    .background {
    margin: 0;
    background-color: #000000;
    background-image: url("CEB Images/bodybackground.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    height: 799px;
}

#menu ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

#menu ul li {
    float: left;
    width: 150px;
    height: 50px;
    line-height: 30px;
    margin: 0;
    margin: 5px 5px;
    text-align: center;
}
#menu ul li:hover {
    background-color: #000066;
    border: double thick #000;
}

#menu ul li a {
    text-decoration: none;
    color: #0099FF;
}

#menu ul li li {
    color: #0099FF;
    display: block;
    margin: 0;
    background-color: #000066;
    height: 50px;
}

#menu ul li li a {
    text-decoration: none;
    color: #0099FF;
}

Upvotes: 0

Views: 331

Answers (2)

Kevin Boucher
Kevin Boucher

Reputation: 16675

You are missing the equals sign before the class name in your link tag:

<li><a class"drop-menu" href="#" >Galleries</a>
            ^ /* Add equals sign: */

<li><a class="drop-menu" href="#" >Galleries</a>

.. therefore the .drop-menu selector is not returning anything.

You should consider storing your selections too:

var main = function() {
    var dropDown = $('#dropdown'),
        dropMenu = $('.drop-menu');

    dropDown.hide();

    dropMenu.mouseenter(function() {
        dropDown.slideDown();
    });
};

This way you don't have to perform expensive DOM lookups every time the user hovers over the menu item.

Upvotes: 1

Sudhansu Choudhary
Sudhansu Choudhary

Reputation: 3360

You had some typos, I've corrected them, and have added a mouseout function also here you go,

<body>
    <div class="background">
        <div id="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Investments</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
                <li><a class="drop-menu" href="#" >Galleries</a>
                <ul id="dropdown">
                    <li><a href="#">Weddings</a></li>
                    <li><a href="#">Engagements</a></li>
                    <li><a href="#">Birthdays</a></li>
                    <li><a href="#">Family</a></li>
                </ul>
                </li>
            </ul>
        </div>
    </div>
<script type="text/javascript">
    var main = function() {
        $('#dropdown').hide();

        $('.drop-menu').mouseenter(function() {
            $('#dropdown').slideDown();
        });
        $('.drop-menu').mouseout(function() {
            $('#dropdown').slideUp();
        });
    };

    $(document).ready(main);
</script>
</body>

works fine!!

Upvotes: 0

Related Questions