I have implemented an accordion menu on my website. However, I am facing an issue where the 2nd menu always slides up when it gets loaded instead of sliding up after clicking on it. I want the behavior to change so that the sub menus elaborate only upon clicking. I'm not sure how to achieve this. Below is the JavaScript code I am currently using:
function initMenu() {
$('#menu ul').hide();
$('#menu ul:first').show();
$('#menu li a').click(function() {
var checkElement = $(this).next();
if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
return true;
}
if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#menu ul:visible').slideUp('normal');
checkElement.slideDown('normal');
return false;
}
});
}
$(document).ready(function() {
initMenu();
});
And here is the corresponding CSS:
#menu{
font-family:'Open Sans';
font-size:13px;
text-align:center;
text-transform:uppercase;
line-height:15px;
width:250px;
font-weight:bold;
}
ul#menu, ul#menu ul {
list-style-type:none;
margin: 0;
padding: 0;
padding-top:5px;
padding-bottom:0px;
width: 15em;
background:url(images/button_menu.png) no-repeat;
line-height:15px;
}
ul#menu a {
display: block;
text-decoration: none;
}
ul#menu li {
margin-top: 1px;
}
ul#menu li a {
line-height:15px;
text-transform:uppercase;
color: #fff;
padding: 0.5em;
line-height:20px;
}
ul#menu li a:hover {
background:url(images/button_menu_hover.png) no-repeat;
}
ul#menu li ul li a {
background:#F7F7F7;
color: #000;
padding-top: 3px;
padding-bottom: 3px;
line-height: 14px;
padding-left: 10px;
border-bottom:0px;
line-height:20px;
}
ul#menu li ul li a:hover {
background:url(images/button_menu_hover.png) no-repeat;
border-left: 5px #006600 solid;
padding-left: 10px;
color:#fff;
}
If anyone could provide guidance on how to resolve this issue, I would greatly appreciate it.