<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
ul, #myUL {
list-style-type: none;
}
#myUL {
margin: 0;
padding: 0;
}
.caret {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
.caret-down::before {
-ms-transform: rotate(90deg); /* IE 9 */
-webkit-transform: rotate(90deg); /* Safari */'
transform: rotate(90deg);
}
.nested {
display: none;
}
.active {
display: block;
}
</style>
</head>
<body>
<h2>Tree View</h2>
<p>A tree view represents a hierarchical view of information, where each item can have a number of subitems.</p>
<p>Click on the arrow(s) to open or close the tree branches.</p>
<ul id="myUL">
<li><span class="caret">Beverages</span>
<ul class="nested">
<li><span class="caret">1AA</span>
<ul class="nested">
<li>2AA1</li>
<li><span class="caret">2AA2</span>
<ul class="nested">
<li>3AA21</li>
<li>3AA22</li>
</ul>
</li>
<li>2AA3</li>
</ul>
</li>
<li><span class="caret">1BB</span>
<ul class="nested">
<li>2BB1</li>
<li><span class="caret">2BB2</span>
<ul class="nested">
<li>3BB21</li>
<li>3BB22</li>
<li>3BB23</li>
</ul>
</li>
</ul>
</li>
<li><span class="caret">1CC</span>
<ul class="nested">
<li>2CC1</li>
<li>2CC2</li>
</ul>
</li>
</ul>
</li>
</ul>
<script>
var toggler = document.getElementsByClassName("caret");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
// alert(this.tagName+ this.parentElement.tagName+this.parentElement.querySelector(".nested").tagName);
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
</script>
</body>
</html>