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

    }

}
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
I set the atrr value and then via for loop I check wich checkbox has value=active and attr checked==false.

HTML:

  <input type="checkbox" checked="" value="active" />
  <input type="checkbox" checked="" value=""/>
  <input type="checkbox" checked="" value=""/>
  <input type="checkbox" checked="" value="active"/>
  <input type="checkbox" checked="" value=""/>
  <input type="checkbox" checked="" value=""/>
  <br/>
  <button id="myButton">Click Me</button>

Open in new window


Javascript:

var chkBoxes=document.getElementsByTagName('input');
var myButton=document.getElementById('myButton');

 myButton.addEventListener('click', function(){
  for (var i=0;i<=chkBoxes.length;i++){
  if(chkBoxes[i].value=='active' && chkBoxes[i].checked===false ){
   chkBoxes[i].checked=true;
  }
}
});

Open in new window



Alternative instead of value attr you can use the data atrr at HTML 5. (data attr MDN)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maheshwar RSoftware DeveloperCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.