Hi there, I am working on an accordion, I am more interested in the way it works, but I am having some problems. I worked on something similar sometime ago, and got some help in here, but I thought I will try to do it again on my own..and ehm, something went wrong.
There must be an error in the code somewhere but I am not sure what it is
as jquery I am using
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
Here's the code I came up with;
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = "text/javascript" src = "script.js"></script>
</head>
<body>
<div id = "accordion">
<ul id = "main_list">
<li><a href = "#" class = "header">Today</a>
<ul>
<li>apples</li>
<li>pears</li>
<li>banabas</li>
</ul>
</li>
<li><a href = "#" class = "header">Yesterday</a>
<ul>
<li>fish</li>
<li>pork</li>
<li>beef</li>
</ul>
</li>
<li><a href = "#" class = "header">Day before yesterday</a>
<ul>
<li>beans</li>
<li>peas</li>
<li>lettuce </li>
</ul>
</li>
</ul>
</div>
</body>
</html>
CSS
#accordion
{
border:1px solid red;
width:500px;
height:500px;
margin:0 auto;
}
#main_list
{
border:3px solid blue;
}
ul#main_list, #main_list ul
{
list-style:none;
}
#main_list li a
{
text-decoration:none;
color:black;
font-size:150%;
}
#main_list li a:hover
{
background-color:red;
}
#main_list > li > ul > li
{
display:none;
}
JQUERY
$(document).ready(function(){
$("ul#main_list a.header").click(function(){
$(this).children("ul").slideToggle();
});
});
Here's a preview: http://antobbo.webspace.virginmedia.com/various_tests/accordion/test.htm
I mean if I look at the script I can't find anything wrong with it: I select the link with a class of header within a list with an id of main_list, attach a click function to it and then using $(this).children("ul").slideToggle();
I refer to the nested list which should toggle...
Interestingly enough if rather than use an external javascript as I have done above, I use an inline script, firebug doesn't find any error anymore, but the script still doesn't work.
I have also tried other things, like this modified version of the script:
$(document).ready(function(){
$("ul#main_list a.header").click(function(){
$(this).children("ul").children("li").slideToggle();
});
});