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

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

here you go :

<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>

working sample is attached to this post.

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()" /><br />
        <br />
        <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>

Hi Serkan,

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

Thanks =)

Be generous to add to my reputation :)

I tried to, but how can I do it?

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.

Added =)

Thanks :)

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.