[RESOLVED]check all checkboxes

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?

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.

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…

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

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

-firstdigit:ok;

-switch:ok;

-"chk"+….:ok;

getElementById  retrieves a null checkbox.

i solved this issue by doing,

chk.setValue(true);

instead of chk.checked=true; 

Leave a Reply