We help IT Professionals succeed at work.

How to check if a check box is active using Java Script?

brgdotnet
brgdotnet asked
on
171 Views
Last Modified: 2017-03-20
I am able to check  all check boxes in my GridView control using Java script. When I press a button it calls my  java script "checkAll".
It works great however I need to change the code so that it first checks if a check box control is active before setting it to a value of true.
How can I do that? There is no "Active" or "Enabled" property, so I can't take that approach :  inputList[i].checked.enabled

So how can I check if a check box is enabled using Java Script



<script type = "text/javascript">
function checkAll(objRef)
{
    var GridView = objRef.parentNode.parentNode.parentNode;
    var inputList = GridView.getElementsByTagName("input");
    for (var i=0;i<inputList.length;i++)
    {
        //Get the Cell To find out ColumnIndex
        var row = inputList[i].parentNode.parentNode;
        if(inputList[i].type == "checkbox")
        {
                inputList[i].checked=true;
        }

    }

}
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Maheshwar RSoftware Developer

Commented:
Hi,

i'm using the following code to enable textbox if the checkbox value is checked, you can change the code to suit your needs

    $("[id*=chkRow]").bind("click", function () {
                //Find and reference the GridView.
                var grid = $(this).closest("table");
 
                //Find and reference the Header CheckBox.
                var chkHeader = $("[id*=chkHeader]", grid);
 
                //If the CheckBox is Checked then enable the TextBoxes in thr Row.
                if (!$(this).is(":checked")) {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#FFF" });
                    $("input[type=text]", td).attr("disabled", "disabled");
                } else {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#D8EBF2" });
                    $("input[type=text]", td).removeAttr("disabled");
                } 
                //Enable Header Row CheckBox if all the Row CheckBoxes are checked and vice versa.
                if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
                    chkHeader.attr("checked", "checked");
                } else {
                    chkHeader.removeAttr("checked");
                }
            });

Open in new window


chkRow is the checkbox id which is in grid template field