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

Dani AI

Generated

Good catch by on the plural API and by calling out the typos. Two extra tips to make this robust with ASP.NET TreeView: (1) scope your toggle to the TreeView’s rendered client ID so you do not touch unrelated checkboxes on the page, and (2) if you truly mean visible only, skip boxes that are hidden (collapsed nodes often remain in the DOM but are not visible). Also, for anyone reading later, there is no getElementsById as suggested by ; the DOM exposes getElementById (singular).

Here is a compact, modern approach that only affects visible, enabled checkboxes inside a specific TreeView:

<script>
function toggleTreeViewChecks(tvClientId, check) {
  var container = document.getElementById(tvClientId);
  if (!container) return false;

  var boxes = container.querySelectorAll('input[type=checkbox]');
  var apply = function(box){
    if (box.offsetParent !== null && !box.disabled) {
      box.checked = !!check;
    }
  };
  // Support NodeList in older browsers
  (boxes.forEach ? boxes.forEach(apply) : Array.prototype.forEach.call(boxes, apply));
  return false;
}
</script>

<button type="button" onclick="toggleTreeViewChecks('<%= TreeView1.ClientID %>', true)">Select all</button>
<button type="button" onclick="toggleTreeViewChecks('<%= TreeView1.ClientID %>', false)">Clear</button>

Notes:

  • '<%= TreeView1.ClientID %>' ensures you target the correct rendered ID, even inside naming containers.
  • offsetParent !== null is a simple visibility check so collapsed items are not toggled.
  • If you update the TreeView via UpdatePanel, rewire any external event handlers after partial postbacks using the MS AJAX hook:
<script>
Sys.Application.add_load(function () {
  // reattach handlers or re-run setup if needed
});
</script>

Recommended Answers

All 12 Replies

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.