Need tree with plus and minus sign
my expected design was like this
+ if the tree node was not clicked and if it is clicked and expanded it should show as -
<!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;
}
.box {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.box::before {
content: "\2610";
color: black;
display: inline-block;
margin-right: 6px;
}
.check-box::before {
content: "\2611";
color: dodgerblue;
}
.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 box(es) to open or close the tree branches.</p>
<ul id="myUL">
<li><span class="box">Beverages</span>
<ul class="nested">
<li><span class="box">Cold beverages</span></li>
<li><span class="box">Hot Beverages</span></li>
</ul>
</li>
<li><span class="box">Tea</span>
<ul class="nested">
<li><span class="box">Black Tea</span></li>
<li><span class="box">White Tea</span></li>
</ul>
</li>
<li><span class="box">Green Tea</span>
<ul class="nested">
<li><span class="box">Sencha</span></li>
<li><span class="box">Gyokuro</span></li>
</ul>
</li>
</ul>
<script>
var toggler = document.getElementsByClassName("box");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("check-box");
});
}
</script>
</body>
</html>