I'm trying to make a script that collapses and expands when you either press the text or the image. The images is a basic arrow, which is being replaced by an arrow pointing upwards when the div is expanded.
My problem is that the text disappears when I toggle the div. How should I write my code, so the headline would stay, no matter if the div is expanded or not? I would like to use the following script on several divs.
Furthermore the slideToggle() function does not work both ways, how do I solve this?
<script type="text/javascript">
function toggle(divId, switchImgTag) {
var ele = document.getElementById(divId);
var imageEle = document.getElementById(switchImgTag);
$("#" + divId).slideToggle("fast");
if (ele.style.display == "block") {
ele.style.display = "none";
imageEle.innerHTML = '<img src="images/arrow_down.png">';
}
else {
ele.style.display = "block";
imageEle.innerHTML = '<img src="images/arrow_up.png">';
}
}
</script>
And the HTML:
<a id='imageDivLink' href="javascript:toggle('skills', 'imageDivLink');">
<h2>Skills And Expertise</h2>
<img class='arrow' src='images/arrow_down.png'>
</a>
<div id='skills' style='display:none;'>
Lorem Ipsum...
</div>