Avatar of brgdotnet
brgdotnet
Flag for United States of America asked on

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

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;
        }

    }

}
JavaScript

Avatar of undefined
Last Comment
Maheshwar R

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Leonidas Dosas

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Maheshwar R

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
Your help has saved me hundreds of hours of internet surfing.
fblack61