Reputation: 1233
I've found this jQuery show submenu if parent have been clicked but when I tried to incorporate it to my pre-made HTML page, the code does not seem to worked in a sense that it shows the submenu right away and it does not toggle to hide the sub menus, I have tried to create a separate JSP file and link it to my main HTML page but it does not work
<script src="test.js"></script>
so I just added this script below to my HTML page, which does not work as well
<script>
$('.sub-menu').hide();
$("li:has(ul)").click(function(){
$("ul",this).toggle('slow');
});
</script>
I am not well versed with JS coding so I would need some guidance on how I can incorporate the codes properly, below is my code where I have added the code that I'm trying to use
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
$(document).ready(function(){
$('.sub-menu').hide();
$("li:has(ul)").click(function(){
$("ul",this).toggle('slow');
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
<div id="container">
<div id="header" style="background-color:#7BD12E">
<h1 align="Left" style="color:#FFF; font-size: 18px;"><strong>Test</strong></h1>
<!-- end #header --></div>
<div id="mainContent">
<ul>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a>
<ul class="sub-menu">
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a>
<ul class="sub-menu">
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</li>
<li><a href="#">Item</a></li>
</ul>
<!-- end #mainContent -->
</div>
<div id="footer" style="background-color:#7BD12E">
<p style="color:#FFF">Test</p>
<!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
Upvotes: 0
Views: 690
Reputation: 58422
you need to wrap the jquery in a $(document).ready()
:
$( document ).ready(function() {
$('.sub-menu').hide();
$("li:has(ul)").click(function(){
$("ul",this).toggle('slow');
});
})
and i would move it to just before your closing </body>
tag as well
and as per BrettL's comments, you need to add the jquery library
Upvotes: 3