By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Select checkboxes on a HTML treeview with JavaScript
JavaScript

Javascript – Select checkboxes on a HTML treeview with JavaScript

Admin
Last updated: 2024/02/10 at 1:10 PM
Admin
Share
4 Min Read
Select checkboxes on a HTML treeview with JavaScript

Problem:

I’m currently deveoling an asp.net core web app with html and javascript.
The problem is that I’m trying to implement a treeview where each element is a checkbox, and when I click an element I want the child elements to be also selected.
You can see an example below:
Example

Contents
Problem:Solution:

And here is the code:

@using ProvaFormularis.Assets.TVItems;

@model IEnumerable<MeasurementModel>

@{
    ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>


<style>
    /* Remove default bullets */
    ul, #myUL {
        list-style-type: none;
    }

    /* Remove margins and padding from the parent ul */
    #myUL {
        margin: 0;
        padding: 0;
        overflow-y: scroll;
        height: 450px;
        background: #f9f9f9;
    }

    /* Style the caret/arrow */
    .caret {
        cursor: pointer;
        user-select: none; /* Prevent text selection */
        font-size: large;
    }

        /* Create the caret/arrow with a unicode, and style it */
        .caret::before {
            content: "\25B6";
            color: black;
            display: inline-block;
            margin-right: 6px;
            font-size: large;
        }

    /* Rotate the caret/arrow icon when clicked on (using JavaScript) */
    .caret-down::before {
        transform: rotate(90deg);
    }

    /* Hide the nested list */
    .nested {
        display: none;
    }

    /* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
    .active {
        display: block;
    }

    li {
        padding: 12px;
    }

    label {
        font-size: large
    }

    input[type=checkbox] {
        transform: scale(1.5);
    }
</style>


<form>
    <ul>
        @foreach (TVStandard tvstandard in StaticClass.TVList.StandardsList)
        {
            <li>
                <span class="caret"></span>
                <input type="checkbox" class="checker" id=@tvstandard.Name name="Standard" value=@tvstandard.folderName unchecked>
                <label for=@tvstandard.Name>@tvstandard.Name (@tvstandard.folderName)</label>
                <ul class="nested">
                    @foreach(TVSection tvsection in tvstandard.Children)
                    {
                        <li>
                            <span class="caret"></span>
                            <input type="checkbox" class="checker" id=@tvsection.Name name="Section" value=@tvsection.scriptName unchecked>
                            <label for=@tvsection.Name>@tvsection.Name (@tvsection.scriptName)</label>
                            <ul class="nested">
                                @foreach (TVMode tvmode in tvsection.Children) 
                                {
                                    <li>
                                        <span class="caret"></span>
                                        <input type="checkbox" class="checker" id=@tvmode.Name name="Mode" value=@tvmode.Name unchecked>
                                        <label for=@tvmode.Name>@tvmode.Name</label>
                                        <ul class="nested">
                                            @foreach (TVFreq tvfreq in tvmode.Children) 
                                            {
                                                <li>
                                                    <input type="checkbox" class="checker" id=@tvfreq.Name name="Frequency" value=@tvfreq.ID unchecked>
                                                    <label for=@tvfreq.Name>@tvfreq.Name (@tvfreq.ID)</label>
                                                </li>
                                            }
                                        </ul>
                                    </li>
                                }
                            </ul>
                        </li>
                    }
                </ul>
            </li>
        }
    </ul>
</form>

<script>
    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");
        });
    }
</script>

<script>
    var toggler = document.getElementsByClassName("checker");
    var i;
    for (i = 0; i < toggler.length; i++) {
        toggler[i].addEventListener("click", function () {
            var lis = this.parentElement.getElementsByClassName(".nested").children;
            var j;
            for (j = 0; j < lis.length) {
                var toggler2 = lis.getElementsByClassName("checker");
                var k;
                for (k = 0; k < toggler2.length; k++) { 

                    toggler2[k].click();
                }
            }
        });
    }
</script>

So, does anybody know how to correctly implement this behavior?

Please note that the first script is the one that deplows the child elements and the second one is the one that must select the childs elements and does not work.


PD: I’ve tested the correct retrieval of click event and the click action of the checkboxes and it works fine. I’ve done that with these scripts:

<script>
    var toggler = document.querySelectorAll("input[type=checkbox]");
    var i;
    for (i = 0; i < toggler.length; i++) {
        toggler[i].click();
    }
</script>
<script>
    var toggler = document.querySelectorAll("input[type=checkbox]");
    var i;
    for (i = 0; i < toggler.length; i++) {
        toggler[i].addEventListener("click", function () {
            this.checked = true;
        });
    }
</script>

Solution:

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Asserting Vuetify v-text-field value using Cypress Javascript – Asserting Vuetify v-text-field value using Cypress
Next Article How to turn off time picker in tempusDominus Javascript – How to turn off time picker in tempusDominus
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?