Click here to Skip to main content
15,883,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a menu with javascript.
My code below shows the div when mouseenter and hides again when mouseenter again.
I want to add more menu items (eg. item1, item2, item3...) .
please help me.


HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<style>
.slidingDiv {
height:300px;
background-color: #99CCFF;
padding:20px;
margin-top:10px;
border-bottom:5px solid #3399FF;
}
 
.show_hide {
display:none;
}
 
</style>
<script type="text/javascript">
 
$(document).ready(function () {
 
$(".slidingDiv").hide();
$(".show_hide").show();
 
$('a.show_hide').mouseenter(function () {
$(".slidingDiv").slideToggle();
 
});
});
 
</script>
 
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" class="show_hide">Item 1</a>
<div class="slidingDiv">
content 1 <a href="#" class="show_hide">hide</a>
</div></div>
 
</form>
</body>
</html>
Posted
Updated 15-Sep-14 7:18am
v2

1 solution

HTML
<html>
    <head>
        <style>
            .slidingDiv {
                height:300px;
                background-color: #99CCFF;
                padding:20px;
                margin-top:10px;
                border-bottom:5px solid #3399FF;
            }
 
            .show_hide {
                display:none;
            }
 
        </style> 
        <script type="text/javascript">
            $(document).ready(function () {
                $(".slidingDiv").hide();
                $(".show_hide").show();

                $('a.show_hide').mouseenter(function (event) {
                    var slide = $(event.target).parent().find('.slidingDiv');
                    if (slide.attr('class') == undefined) {
                        slide = $(event.target).parent();
                    }
                    slide.slideToggle();
                });
            });
        </script>
    </head>
    <body>
        

        <form id="form1" runat="server">
            <div>
                <a href="#" class="show_hide">Item 1</a>
                <div class="slidingDiv">
                    content 1 <a href="#" class="show_hide">hide</a>
                </div></div> 
            <div>
                <a href="#" class="show_hide">Item 2</a>
                <div class="slidingDiv">
                    content 2 <a href="#" class="show_hide">hide</a>
                </div></div>
        </form>
    </body>
</html>
 
Share this answer
 
Comments
Member 10316149 15-Sep-14 10:02am    
@Nathan.. thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900