Check/uncheck checkboxes in TreeView using javascript

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Check/uncheck checkboxes in TreeView using javascript

 
0
  #1
Sep 17th, 2009
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:

  1. function selectAllNone(tvID, value) {
  2. var tvNodes = document.getElementById(tvID);
  3. var chBoxes = tvNodes.getElementByTagName("input");
  4. for (var i = 0; i < chBoxes.length; i++) {
  5. var chk = chkBox[i];
  6. if (chk.type == "checkbox") {
  7. chk.cheked = value;
  8. }
  9. }
  10. }

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
1
  #2
Sep 17th, 2009
i think that method is not getElementByTagName, but getElementsByTagName, so it is plural basically. try that way.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #3
Sep 17th, 2009
here you go :

<script language="javascript" type="text/javascript">
// <!CDATA[

  1. function Button1_onclick() {
  2. var inputs = document.forms[0].elements;
  3. for(var i=0;i< inputs.length;i++)
  4. {
  5. inputs[i].checked = true;
  6. }
  7. }
  8.  
  9. function Button2_onclick() {
  10. var inputs = document.forms[0].elements;
  11. for(var i=0;i< inputs.length;i++)
  12. {
  13. inputs[i].checked = false;
  14. }
  15. }
  16.  
  17. // ]]>
  18. </script>
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #4
Sep 17th, 2009
working sample is attached to this post.
Attached Files
File Type: zip WebSite2.zip (1.9 KB, 2 views)
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #5
Sep 17th, 2009
Just in case you cant run the attachment :
Default.aspx :
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. <script language="javascript" type="text/javascript">
  9. // <!CDATA[
  10.  
  11. function Button1_onclick() {
  12. var inputs = document.forms[0].elements;
  13. for(var i=0;i< inputs.length;i++)
  14. {
  15. inputs[i].checked = true;
  16. }
  17. }
  18.  
  19. function Button2_onclick() {
  20. var inputs = document.forms[0].elements;
  21. for(var i=0;i< inputs.length;i++)
  22. {
  23. inputs[i].checked = false;
  24. }
  25. }
  26.  
  27. // ]]>
  28. </script>
  29. </head>
  30. <body>
  31. <form id="form1" runat="server">
  32. <div>
  33. &nbsp;<input id="Button1" type="button" value="Select All" onclick="Button1_onclick()" />
  34. <input id="Button2" type="button" value="Deselect All" onclick="return Button2_onclick()" /><br />
  35. <br />
  36. <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
  37. <Nodes>
  38. <asp:TreeNode Text="New Node" Value="New Node">
  39. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  40. </asp:TreeNode>
  41. <asp:TreeNode Text="New Node" Value="New Node">
  42. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  43. </asp:TreeNode>
  44. <asp:TreeNode Text="New Node" Value="New Node">
  45. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  46. </asp:TreeNode>
  47. </Nodes>
  48. </asp:TreeView>
  49. &nbsp;</div>
  50. </form>
  51. </body>
  52. </html>
Last edited by serkan sendur; Sep 17th, 2009 at 8:36 pm.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #6
Sep 18th, 2009
Hi Serkan,

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

Thanks =)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #7
Sep 18th, 2009
Be generous to add to my reputation
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #8
Sep 18th, 2009
I tried to, but how can I do it?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #9
Sep 18th, 2009
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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 69
Reputation: Ana D. is an unknown quantity at this point 
Solved Threads: 0
Ana D. Ana D. is offline Offline
Junior Poster in Training

Re: Check/uncheck checkboxes in TreeView using javascript

 
0
  #10
Sep 18th, 2009
Added =)
Reply With Quote Quick reply to this message  
Reply

Tags
asp.net, checkbox, javascript, treeview

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC