Click here to Skip to main content
14,427,367 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a treeview in asp.net with dynamic content populated at run time , i want to select just one node for which i copied java script from online to do this which works, as i am new in c# and java script , but it has one issue that it works at page level i.e it also uncheck all other checkbox controls in that same page.

What i want is this java script to work at only this treeview item only instead of all control at page level, i tried by specifying treeview as tag name in JS function but didnt work , please help me in this regard as to what to change exactly

My treeview is defined like this in aspx page :
<asp:TreeView ID="tvwItems"

and this is called like below in c# code file at page render level event :
tvwItems.Attributes.Add("OnClick","client_OnTreeNodeChecked(event)");



The java script is as following :

What I have tried:

<script type="text/javascript">
    // select only one folder from treeview
    function client_OnTreeNodeChecked(event) {
        var treeNode = event.srcElement || event.target;
        if (treeNode.tagName == "INPUT" && treeNode.type == "checkbox") {
            if (treeNode.checked) {
                uncheckOthers(treeNode.id);
            }
        }
    }

    function uncheckOthers(id) {
        var elements = document.getElementsByTagName('input');
        // loop through all input elements in form -- issue here need to limit to that tree only , help required from expert ??
        for (var i = 0; i < elements.length; i++) {
            if (elements.item(i).type == "checkbox") {
                if (elements.item(i).id != id) {
                    elements.item(i).checked = false;
                }
            }
        }
    }
     </script>


Many thanks
Chandan
Posted
Updated 8-Jan-20 16:54pm
Comments
ZurdoDev 8-Jan-20 11:46am
   
First off you really need to understand how the code works and then it will be easy to fix. It's dangerous to use code you found on the internet but do not understand.

It loops through the elements with a different id and unchecks them.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Thanks for your hint ,i added element name with this syntax and now its works as expected

var elements = document.getElementById("<% = tvwItems.ClientID %>").getElementsByTagName('input');
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100