OnClick List Open Using Jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>A Nested List</h2>
<div id="mylist">
<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
    <li>Black tea</li>
    <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>
</div>
/* CSS */
ul li ul {
  display: none; /* Hide the nested list first */
}
ul li.active ul {
  display: block; /* Show the list when class 'active' is added to the li */ 
  color: red;
}
// JavaScript
jQuery( "li:has(ul)" ).click(function(){ // When a li that has a ul is clicked ...
	jQuery(this).toggleClass('active'); // then toggle (add/remove) the class 'active' on it. 
});
Buy Me A Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s