I need to create a content DIV
that includes a link to expand and collapse it.
Within this content DIV
, there is an unordered list. Initially, only two list items should be displayed with an expand link. If users want to see additional list items, they must click on the expand link. Once expanded, the link text should change to Collapse
. Additionally, if the unordered list only contains two items, the link should not be displayed.
NOTE: The unordered list is generated dynamically using PHP
.
Here is an example of my HTML structure -
<div id="mycontent">
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
</ul>
<p><a href="">+ View More</a><p>
</div>
This is the JQuery code I have implemented -
$("a").click(function(){
$("#mycontent").toggle();
});
You can view the current state of my code here: http://jsbin.com/mojuteve/1/edit
If you have any suggestions on how to improve this functionality, please let me know.
Thank you!