Trying to do accordion style display.
Better reference to the kind of display I want: https://imgur.com/tr9cd52
You will notice to the right of Lily there is an arrow, clicking on it should show the items under.
My challenge is in traversing through the DOM correctly to locate the next ul to show/hide when button is clicked. How I do that?
As I'm sure you can tell what I'm trying to do is show the nested style but in a box to create an accordion effect.
Working fiddle https://jsfiddle.net/zigzag/aLb5d2uL/10/
$('.menu-toggle').click(function(e) { e.preventDefault(); // prevent <a> to redirect to the top of the page $(this).nextall('ul').toggle(); });
.chartArea { padding: 20px 20px 0 0; } ul { list-style-type: none; } .glyphicon { color: white; } .tile { margin: 10px; padding: 20px; background-color: gray; font-family: segoe UI; color: white; min-height: 150px; } .empDetails { font-size: 14px; color: white; } .chartArea ul li { display: none; } .chartArea>ul>li { display: block; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="container-fluid chartArea"> <ul> <li> <div class="tile">Adam</div> </li> <li> <div class="tile"> <div class="col-sm-2 img_tile"> <img src="http://via.placeholder.com/50x50"> </div> <div class="col-sm-8 empDetails"> <h4> Lily </h4> <p> Director </p> <p> Ensure quality and timely delivery through resource and time management. </p> </div> <div class="col-sm-2 toggle_button"> <a class="menu-toggle" href="#"> <span class="glyphicon glyphicon-menu-down"></span> </a> </div> </div> <ul> <li> <div class="tile"> <div class="col-sm-2 img_tile"> <img src="http://via.placeholder.com/50x50"> </div> <div class="col-sm-8 empDetails"> <h4> Sen </h4> <p> Manager </p> <p> Ensure quality and timely delivery through resource and time management. </p> </div> </div> </li> <li> <div class="tile">Another Sen</div> <ul> <li> <div class="tile">Sen jr</div> </li> </ul> </li> </ul> </li> <li class="tile">Justin</li> </ul> </div>
Most of the code is posted above. I have some nested UL Li tags but can't find a way to create a logic that collapses or shows the list of objects around the click each time.
A number of things are going on here.
.chartArea ul { display: none; }
.chartArea ul { display: none; }
to .chartArea ul li ul {display: none; }
.chartArea ul li ul {display: none; }
so that only the <ul> inside the <li> is hidden. I would actually recommend you change your HMTL markup a bit to make the DOM navigation easier, but to do this with your markup, you need can do the following things:
Here's the Fiddle. Note that I changed your jQuery and the one CSS rule. https://jsfiddle.net/f4m22hmy/1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.