954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Check/uncheck checkboxes in TreeView using javascript

Hi,

In my ASP.NET page I have a TreeView where ShowCheckBoxes="Leaf".
I want to mark as checked/unchecked all the visible checkboxes by clicking in a button.

My code for selecting all/none nodes is like this:

function selectAllNone(tvID, value) {
            var tvNodes = document.getElementById(tvID);
            var chBoxes = tvNodes.getElementByTagName("input");
            for (var i = 0; i < chBoxes.length; i++) {
                var chk = chkBox[i];
                if (chk.type == "checkbox") {
                    chk.cheked = value;
                }
            }        
        }


However, in the line var chBoxes =tvNodes.getElementByTagName("input");I'm having this error message: object doesn't support this property or method.
How do I checj/uncheck all the checkboxes, then?

Thanks,

Ana

Ana D.
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

i think that method is not getElementByTagName, but getElementsByTagName, so it is plural basically. try that way.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

here you go :

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

working sample is attached to this post.

Attachments WebSite2.zip (1.94KB)
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Just in case you cant run the attachment :
Default.aspx :

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {
var inputs = document.forms[0].elements;
for(var i=0;i< inputs.length;i++)
{
inputs[i].checked = true;
}
}

function Button2_onclick() {
var inputs = document.forms[0].elements;
for(var i=0;i< inputs.length;i++)
{
inputs[i].checked = false;
}
}

// ]]>
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<input id="Button1" type="button" value="Select All" onclick="Button1_onclick()" />
        <input id="Button2" type="button" value="Deselect All" onclick="return Button2_onclick()" />
        
        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
            <Nodes>
                <asp:TreeNode Text="New Node" Value="New Node">
                    <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="New Node" Value="New Node">
                    <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="New Node" Value="New Node">
                    <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
        &nbsp;</div>
    </form>
</body>
</html>
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Hi Serkan,

You were right: the method is getElementsByTagName. I just changed it and it's working.

Thanks =)

Ana D.
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Be generous to add to my reputation :)

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

I tried to, but how can I do it?

Ana D.
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

in the beginning of each post there is a "add to serkan's reputation" link , you basically click on it and then select agree radio button then type something then click ok.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Added =)

Ana D.
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Thanks :)

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Looks simple.
Change
var tvNodes = document.getElementById(tvID);
to
var tvNodes = document.getElementsById(tvID);

Hope It worked.Do tell Us.
Happy Coding.

Hi,

In my ASP.NET page I have a TreeView where ShowCheckBoxes="Leaf". I want to mark as checked/unchecked all the visible checkboxes by clicking in a button.

My code for selecting all/none nodes is like this:

function selectAllNone(tvID, value) {
            var tvNodes = document.getElementById(tvID);
            var chBoxes = tvNodes.getElementByTagName("input");
            for (var i = 0; i < chBoxes.length; i++) {
                var chk = chkBox[i];
                if (chk.type == "checkbox") {
                    chk.cheked = value;
                }
            }        
        }

However, in the line var chBoxes =tvNodes.getElementByTagName("input");I'm having this error message: object doesn't support this property or method. How do I checj/uncheck all the checkboxes, then?

Thanks,

Ana

WhiteShadows
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Hi,

Just use the below code. The first one have lot of spelling mistakes.

function selectAllNone(value) {
            var tvNodes = document.getElementById("trvMenu");
            var chBoxes = tvNodes.getElementsByTagName("input");
            for (var i = 0; i < chBoxes.length; i++) {
                var chk = chBoxes[i];
                if (chk.type == "checkbox") {
                    chk.checked = value;
                }
            }
            return false;
        }

Thanks and Regards,

Ananth N

ananthat
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: