Nisha Patel
Problem
I'm currently developing 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:
And here is the code:
So, does anybody know how to correctly implement this behavior?
Please note that the first script is the one that deploys the child elements and the second one is the one that must select the child 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:
Solution
In your second script, you have a syntax error in the for loop. It should be for (j = 0; j < lis.length; j++) instead of for (j = 0; j < lis.length).
Instead of using .getElementsByClassName(), you should use .querySelectorAll() to select elements by class name.
When you use lis.getElementsByClassName("checker"), it will return an HTMLCollection, so you'll need to iterate through it.
Suggested blogs:
>Javascript Error Solved: Property 'id' does not exist on type 'T'
>Why highlighted table row using class not working in JavaScript?
>How to rename an object key based on the condition in JavaScript?
>How to sort an array based on another array in Javascript?
>Javascript: Modal not closing with a button