Reputation: 2295
I am trying to expand/collapse list items. Text will be word followed by definition sort of where definition appears/disappears on click on word. This is my code so far :
<html>
<script type="text/javascript">
function toggleVisibility(listItem) {
var listItemDiv = document.getElementById(listItem);
if(listItemDiv.style.visibility == 'hidden') {
listItemDiv.style.visibility = 'visible';
} else {
listItemDiv.style.visibility = 'hidden';
}
}
</script>
<div onclick="toggleVisibility('p1')">p1</div><div id=p1>p1's text</div></br>
p2<div id=p2>p2's text</div></br>
p3<div id=p3>p3's text</div></br>
p4<div id=p4>p4's text</div></br>
p5<div id=p5>p5's text</div></br>
</html>
However this code hides my text isntead of collapse/expand. I wish to achieve this through javascript preferably.
Upvotes: 0
Views: 2881
Reputation: 2295
SOLUTIONS:
function toggleVisibility(listItem) {
var listItemDiv = document.getElementById(listItem);
if(listItemDiv.style.display == 'none') {
listItemDiv.style.display = 'block';
} else {
listItemDiv.style.display = 'none';
}
}
OR
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function(){
$('dt').click(function(){
var dl = $(this).parent();
$('dd',dl).slideToggle();
});
});
</script>
<dl>
<dt>
item1
</dt>
<dd>
item1.description
<dd>
</dl>
</html>
Upvotes: 1