I decided to make a few tweaks to your code. The updated version now functions correctly, aligning with what you intended.
Instead of relying solely on Javascript, I incorporated jQuery for the click and hover events.
For a demonstration, you can access the working code by clicking below:
Click here for DEMO
Below is the snippet of HTML code used:
<div class="accordion">
<div class="headA">
<a href="#">Head</a>
</div>
<div id="accbody" class="accordion-body">
<a href="#">Body</a>
</div>
</div>
The CSS styling applied is as follows:
.accordion {
border: 1px solid #444;
margin-left: 60px;
width: 30%;
}
.accordion:hover div {
display: block;
}
.accordion-body a {
background-color: green;
display: block;
color: white;
padding: 25px;
text-align: center;
}
.headA a {
text-align: center;
display: block;
background-color: yellow;
padding: 25px;
}
And finally, the jQuery script utilized for the functionality:
$(document).ready(function() {
// on page load hide accordion body
var accordionBody = $('#accbody');
accordionBody.hide();
$('.headA').click(function() {
if (accordionBody.is(':hidden')) {
accordionBody.slideDown(400);
} else {
accordionBody.slideUp(400);
}
});
$('.headA').hover(function() {
if (accordionBody.is(':hidden')) {
accordionBody.slideDown(400);
} else {
accordionBody.slideUp(400);
}
});
});