Implement a vertical bar to appear next to the tree view when it is expanded

I am struggling to implement a vertical line for the first level of list items when they are expanded and remove it when they are closed. I have tried using li::after but it doesn't seem to work. Can someone provide guidance on how to achieve this?

The vertical line should only be present along the first level of hierarchy.

I have set up a treeview with three list items and provided the code that needs to be updated to include the vertical lines.

var toggler = document.getElementsByClassName("caret");
var i;

for (i = 0; i < toggler.length; i++) {
  toggler[i].addEventListener("click", function() {
    this.parentElement.querySelector(".nested").classList.toggle("active");
    this.classList.toggle("caret-down");
  });
}
ul, #main {
  list-style-type: none;
}

#myUL {
  margin: 0;
  padding: 0;

}
li::after{
border-left 1px solid black;
}
.caret {
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none; 
  -ms-user-select: none; 
}

.caret::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}

.caret-down::before {
  -ms-transform: rotate(90deg); 
  -webkit-transform: rotate(90deg);
  transform: rotate(90deg);  
}

.nested {
  display: none;
  border-left 1px solid #000
}

.active {
  display: block;
  border-left 1px solid #000
}
<ul id="main">
  <li><span class="caret">First</span>
    <ul class="nested">
      <li>sub1</li>
      <li>sub2</li></ul>
      <li><span class="caret">Second</span>
        <ul class="nested">
          <li>sub1</li>
          <li>sub2</li></ul>
          <li><span class="caret">Third</span>
            <ul class="nested">
              <li>sub1</li>
              <li>sub2</li>
            </ul>
          </li>
      </li>  
  </li>
</ul>

Answer №1

To achieve this effect, utilize the CSS rule .nested.active as shown below:

Include the following CSS code:

.nested.active{
   border-left:1px solid blue;
}

This code snippet demonstrates how it is implemented:

var toggler = document.getElementsByClassName("toggle");
var i;

for (i = 0; i < toggler.length; i++) {
  toggler[i].addEventListener("click", function() {
    this.parentElement.querySelector(".nested").classList.toggle("active");
    this.classList.toggle("toggle-down");
  });
}
ul, #container {
  list-style-type: none;
}

#myList {
  margin: 0;
  padding: 0;

}
li::after{
border-left 1px solid red;
}
.toggle {
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none; 
  -ms-user-select: none; 
}

.toggle::before {
  content: "\25C0";
  color: blue;
  display: inline-block;
  margin-right: 6px;
}

.toggle-down::before {
  -ms-transform: rotate(90deg); 
  -webkit-transform: rotate(90deg);
  transform: rotate(90deg);  
}

.nested {
  display: none;
  border-bottom 1px solid #333
}

.active {
  display: block;
  border-top 1px solid #555
}
.nested.active{
border-left:1px solid green;
}
<ul id="container">
  <li><span class="toggle">First</span>
    <ul class="nested">
      <li>item1</li>
      <li>item2</li></ul>
      <li><span class="toggle">Second</span>
        <ul class="nested">
          <li>item1</li>
          <li>item2</li></ul>
          <li><span class="toggle">Third</span>
            <ul class="nested">
              <li>item1</li>
              <li>item2</li>
            </ul>
          </li>
      </li>  
  </li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Date picker is unresponsive to clicks when inner html is being used

When I tried to use the code below, I found that I am unable to select the data picker while using the inner HTML. Can anyone help me figure out what I am doing wrong? Thanks in advance. Below is my code snippet: $(document).ready(function(){ /*Date Pic ...

There is no response from Ajax

I am facing an issue with my AJAX call in communication with my Rails controller. Even though the AJAX call itself seems fine, the callback function does not contain any data. Here is the AJAX request I'm making: $.ajax({ url: '/get_progres ...

What is the process for determining the date that is 28 days past a specified date?

I need assistance with finding the 28th day from a date formatted as YYYY-MM-DD. I've attempted various methods without success. Ideally, I would prefer a solution that does not involve Moment.js. Check out my code on Jsfiddle If there are no altern ...

Changing textarea html content to an iframe in real time with the use of jQuery

Is it possible to use jQuery to transfer HTML content entered into a textarea to an iframe in real time, without refreshing the page? I have searched for a solution but have not found a clear direction. Any code snippet and explanation would be greatly ap ...

Best practices for coding in HTML and CSS

Creating my first website for an internship has been quite a learning experience. Throughout my training, I was always advised to avoid embedding styles directly into the HTML code. However, now that I am actually developing a site, I can't help but f ...

Short-circuiting async flows on non-error events in Node.js

Node implements the CPS convention for callbacks. Typically, when a function encounters an error, it is common practice to either handle the error or callback the error to the parent function by utilizing something like return cb(err). One challenge I fac ...

Field must have a base type specified to proceed

Currently, I am in the process of developing a discord.js bot. The structure I have set up involves a folder that contains all the commands, and when a command is called, index.js directs it to the appropriate file. However, when attempting to run the bot, ...

The flex reverse-column child's text selection occurred in the incorrect direction

I am having an issue with selecting text in a flex container that is set to reverse column. The selection I make is incorrect because the browser tries to select from the end of the element, resulting in improper selection. For example, <div style=" ...

Is there a modal confirmation feature available in Salesforce?

if ((Modal.confirm && Modal.confirm('some unique text')) || (!Modal.confirm && window.confirm('same but different text'))) navigateToUrl('another link here','ANOTHER DETAIL','submit'); I am curious about t ...

Guide on comparing an object against an array and retrieving a specific output

If I were to create a data structure like this: const carObj = {"1234":"Corvette","4321":"Subaru","8891":"Volvo"}; And also have an array that contains the IDs: const myArray = [1234, 4321, 8891, ...

Establish characteristics for the temporary print document

Struggling to configure the properties of a temporary file created for later printing by the user. Here's a breakdown of the process: User clicks on "Print Map Area" button on the website. A menu appears asking for preferred dimensions (e.g. A4 ...

Angular 4 is displaying an error message indicating that the expression has been modified after being initially verified

I've been utilizing Angular Material within my Angular 4 application. There seems to be an issue when attempting to utilize the MatSnackBar in the ngAfterViewInit(). The error I encounter is as follows: ExpressionChangedAfterItHasBeenCheckedError: ...

Is there a way to have the span update even if the input stays the same? Currently, it only changes when the input is different

Retrieve results of 3 lines (Ps) by entering a word in the text area and clicking search. If the word is found after clicking the button, a span will be displayed with the count of occurrences as well as the highlighted P(s) where it was found. If the wo ...

Is there a simple way to create a clickable popup menu without using JavaScript?

/*---Creating a Popup Menu with CSS---*/ .box{ position:fixed; top: 0.1%; left: 14px; display: inline-block; cursor: pointer; } ul{ list-style-type: none; } ul li{ font-family: Lato; padding: 10px; } ul li a{ text-decoration: none; color: black; } ul li:ho ...

Populate HTML drop-down menu with Java ArrayList

I am looking to implement multiple drop down select in JSF. I came across this code that offers a solution: <select id="dates-field2" class="multiselect-ui form-control" multiple="multiple"> <optio ...

Substitute the website address using regular expressions

Looking to update the iframe URL by changing autoplay=1 to autoplay=0 using regular expressions. jQuery(document).ready(function($){ $('.playButton').click(function(){ $('.flex-active-slide iframe').contents().f ...

Reverting back to the specified CSS style

In my application, I have a common HTML component styled as follows: .box { min-width: 100px; padding: 20px 10px; } There are over 100 of these components and they each have a unique border style without a bottom border: .box:nth-child(1) { border ...

Ensure every individual input field in a Bootstrap form is validated using JavaScript before submitting the form

As a newcomer to Javascript, I am seeking assistance with validating each field in the form below without having to click on the submit button. Your help is greatly appreciated! <form action="" id = "form1" class = "form-group row"> & ...

Using `ng-model` within an Angular directive

Looking to achieve bi-directional binding in an Angular directive Here is my current approach: angular.module('myapp',[]),directive('mydirective', function() { var directive = {}; directive.restrict = 'E'; directi ...

What is the reason for min-content not being compatible with auto-fill or auto-fit?

My confusion lies in the fact that this code snippet works: .grid { display: grid; grid-template-columns: repeat(4, min-content); } Whereas this one does not: .grid { display: grid; grid-template-columns: repeat(auto-fill, min-content); } I am ...