locked
check all checkboxes RRS feed

  • Question

  • User959860059 posted

    i want to check all checkboxes when a checkbox is checked in javascript.

     function Check_Checked(chk, hdnvalue)
        {
            var firstdigit = (chk.tabIndex / 10).toString()[0];
           
            switch (hdnvalue)
            {
      
                case "YarimCene":
                    for (var i = 1; i < 9; i++)
                    {
                        var ch = document.getElementById("chk" + firstdigit + i.toString());
                        ch.checked = true;
                    }
                    break;

    but it does not work.how can do this?

    Wednesday, October 1, 2014 4:35 AM

Answers

  • User959860059 posted

    i solved this issue by doing,

    chk.setValue(true);

    instead of chk.checked=true; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 1, 2014 6:25 AM

All replies

  • User-760709272 posted

    Do you get errors in the error console?  what is chk?  does it have a value?  What is chk.tabIndex? What does "firstdigit" end up being?  What is hdnvalue?  do you have a checkbox that has an id of

    chk<firstdigit>1 all the way to chk<firstdigit>9?

    is ch every anything other than undefined?  Your javascript is very dependent on a lot of things you haven't posted so it is hard to give an accurate answer, you'll need to try a browser like ie or chrome that lets you debug and step through your javascript.

    Wednesday, October 1, 2014 4:45 AM
  • User753101303 posted

    Hi,

    Please always use first the debugger before posting. See https://developer.chrome.com/devtools/docs/javascript-debugging to answer yourself to questions such as :

    - does have firstdigit the value you expect ?
    - does your code enter the appropriate switch option ?
    - is "chk" + firstdigit + i.toString(); the value you expect ?
    - and finally does getElementById really find something (ch is not null ?)

    Then post if all seems and still it doesn't work but basic debugging steps should be done before your first post about a problem...

    Wednesday, October 1, 2014 4:49 AM
  • User571301025 posted

    Can you add some more detail also post your entire aspx/cs code?

    Wednesday, October 1, 2014 4:51 AM
  • User1636150863 posted

    Hi,

    Do you want something like below ??

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function checkAll(ele) {
                 if (ele.checked) {
                     for (var i = 1; i <= 5; i++) {
                         var ch = document.getElementById("chk" + i.toString()); // you can add your check box id here instead of '"chk" + i.toString()'
                         ch.checked = true;
                     }
                 }
                 else {
                     for (var i = 1; i <= 5; i++) {
                         var ch = document.getElementById("chk" + i.toString()); // you can add your check box id here instead of '"chk" + i.toString()'
                         ch.checked = false;
                     }
                 }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table id="dataTable" border="1">
                    <tr>
                        <th>
                            <input type="checkbox" onchange="checkAll(this)" name="chkbox" /> <!-- On onchange of this checkbox you want to check all checkboxes -->
                        </th>
                        <th>
                            <input type="checkbox" id="chk1" />1</th>
                        <th>
                            <input type="checkbox" id="chk2" />2</th>
                        <th>
                            <input type="checkbox" id="chk3" />3</th>
                        <th>
                            <input type="checkbox" id="chk4" />4</th>
                        <th>
                            <input type="checkbox" id="chk5" />5</th>
                    </tr>
                </table>
    
            </div>
        </form>
    </body>
    </html>

    Regards,

    Ajay

    Wednesday, October 1, 2014 5:06 AM
  • User959860059 posted

    -firstdigit:ok;

    -switch:ok;

    -"chk"+....:ok;

    getElementById  retrieves a null checkbox.

    Wednesday, October 1, 2014 5:51 AM
  • User959860059 posted

    i solved this issue by doing,

    chk.setValue(true);

    instead of chk.checked=true; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 1, 2014 6:25 AM