samedi 23 décembre 2017

Checkbox will not fire function onLoad of page

I've been banging my head on this issue for 8 hours straight and I can't figure it out.

Environment: I'm developing a webapp which has a button/switch that toggles a checkbox when clicked/checked. When the checkbox is checked, it fires a function that calls a PHP script. When UNchecked, it pauses the PHP script. This is all working great.

ISSUE: I also have the checkbox configured to be in a checked state when the page loads, thus triggering the function automatically when the page loads. However, right now, when I load the page the checkbox IS checked, but the function DOES not fire automatically UNTIL the checkbox is toggled manually.

I've used A LOT of different variables and solutions, but I can't figure it out. I'm assuming it has something to do with the "Label" class?

What am I doing wrong?

Example: You can ignore the PHP stuff, as I put in an alert for testing. YOu'll the checkbox is checked onload, but the alert does not fire onload but will fire when the checkbox is manually checked.

JSfiddle

<script>
  var nIntervId;
  var onload;

  function statusCheck() {

    $("#statusloop").load('assets/php/loop.php');
    $("#stats").load('assets/php/systembadges.php');

  };

  $(document).ready(function() {

    $(":checkbox").change(function() {
      if ($(this).is(':checked')) {
        nIntervId = setInterval(statusCheck, 3000);
        alert("checked");
      } else {
        clearInterval(nIntervId);
        alert("NOTchecked");
      }
    });
  });

</script>

<body onload="statusCheck()">

  <label class="switch" id="buttonStart">
    <input type="checkbox">
    <span class="slider round"></span>
  </label>

  <script>
    $('#buttonStart :checkbox').attr('checked', 'checked');
  </script>

</body>




Aucun commentaire:

Enregistrer un commentaire