lundi 7 octobre 2019

How to handle initial state and change of checkbox using jQuery?

I need to show/hide div based on checkbox value by jquery:

  1. when webpage is loaded and
  2. when checkbox value is changed (on click/on change)

I found few ways how to solve these problems separately, but does exists a simple way how to handle both group of events at once?

Example (change):

<input class="form-check-input" type="checkbox" id="checkbox1" checked>

<div id="autoUpdate" class="autoUpdate">
    Example
</div>

<script>
    $(document).ready(function(){
        $('#checkbox1').change(function(){
          if(this.checked)
            $('#autoUpdate').show();
          else
            $('#autoUpdate').hide();
        });
    });
</script>



Aucun commentaire:

Enregistrer un commentaire