Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.NET
<asp:TreeView ID="tvwItems"

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



The java script is as following :

What I have tried:

JavaScript
<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 15: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

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

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900