mercredi 10 avril 2019

Set all checkboxes to true after DOM page has loaded

I have multiple checkboxes that are created using for loops with Javascript only once the web page has loaded.

I want to make all the checkboxes set to true by default, I can do this in the console, but when I put it in my script, it doesn't work since the function probably loads before the DOM elements have loaded.

I've tried two different approaches, and neither seem to work. In both cases, they only work for the hardcoded checkboxes I typed up (not generated by for loops/DOM). If anyone knows how I can resolve this, I'd be very grateful!

Here is my basic jQuery/JS code.

$(document).ready(function() {
  var setTrue = document.getElementsByTagName("input");
  for (var i = 0; i < setTrue.length; i++) {
    setTrue[i].checked = true;
  };
});

$(window).on("load", function() {
  var setTrue = document.getElementsByTagName("input");
  for (var i = 0; i < setTrue.length; i++) {
    setTrue[i].checked = true;
  };
});




Aucun commentaire:

Enregistrer un commentaire