Problem with javascript and checkboxes code

This works ok

var limit = document.forms[0].elements.length;
    for (i=0;i<limit;i++) {
      document.forms[0].elements[i].disabled = true;

Open in new window

I need something like this for checkboxes

var limit = document.forms[0].elements.length;
    for (i=0;i<limit;i++) {
      document.forms[0].elements[i].checked = true;

Open in new window

Who is Participating?
WebDevEMConnect With a Mentor Commented:

I found this answer at which seems to work for me:
var getInputs = document.getElementsByTagName("input");
for (var i = 0, max = getInputs.length; i < max; i++){
if (getInputs[i].type === 'checkbox')
getInputs[i].checked = true;

Open in new window

It checks all checkboxes on the page, without regard for their class, id or anything.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.