1

This is my code to make check boxes checked with same id

<input type="checkbox" name="MassApprove" value="setuju2"  />
<input type="checkbox" name="MassApprove" value="setuju3"  />
<input type="checkbox" name="MassApprove" value="setuju4"  />
<input type="checkbox" name="MassApprove" value="setuju5"  />
<input type="submit" value="Next Step" name="next" />

And This is my javascript code. I need to make this checkboxes as checked when am trigger this function. Help me!..

    function doMassApprove(massApproveFlag) {
        var confirmAlert = confirm("Do You Need Mass Approve !..");
        var massApprove = document.getElementById("MassApprove").length();
        if (confirmAlert == true) {
            //alert(massApproveFlag);
            for (var i = 0; i < massApprove; i++) {
                document.getElementById("MassApprove").checked = true;
            }
        } else {
            document.getElementById("headerMassApprove").checked = false;
        }
    }
3
  • 1
    you need to set an "ID" to your elements if you use document.getElementById Commented Jul 31, 2015 at 12:27
  • 2
    Document.getElementsByName() IDs in HTML must be unique Commented Jul 31, 2015 at 12:27
  • 1
    I'm curious to which event and element would you like to attach the function and what was the idea behind the parameter massApproveFlag. Commented Jul 31, 2015 at 12:34

3 Answers 3

1

IDs in HTML must be unique.

As you have already specified the name MassApprove, use Document.getElementsByName(),

Returns a nodelist collection with a given name in the (X)HTML document.

Which you can iterate using simple for loop

function doMassApprove(massApproveFlag) {
    var confirmAlert = confirm("Do You Need Mass Approve !..");

    if (confirmAlert) {
        //Get elements with Name
        var massApproves = document.getElementsByName("MassApprove");

        //Iterate and set checked property
        for (var i = 0; i < massApprove.length; i++) {
            massApproves[i].checked = true;
        }
    } else {
        document.getElementById("headerMassApprove").checked = false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
  1. you do not have the same ID and should not since ID must be unique. You have the same NAME and that is fine. -
  2. Do not user getElementById for names, instead use document.getElementsByName which will return a collection you can loop over

Like this

function doMassApprove(massApproveFlag) {
    var massApprove = confirm("Do You Need Mass Approve !..");
    if (massApprove) {
      var checks = document.getElementsByName("MassApprove");
      for (var i=0; i < checks.length; i++) {
          checks[i].checked = massApprove; // or perhaps massApproveFlag?
      }
    }
    else {
      document.getElementById("headerMassApprove").checked = false;
    }
}

Comments

0

Just for the heck of it, you can use some modern browser goodness:

Array.prototype.forEach.call(document.querySelectorAll('[name=MassApprove]'), function(cb){cb.checked = true});

and with arrow functions:

 Array.prototype.forEach.call(document.querySelectorAll('[name=MassApprove]'), cb => cb.checked=true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.