how to check first checkbox if any other else is checked, uncheck first checkbox if all other is unchecked


<div class="container">
  <!-- <h2> check first checkbox </h2> -->
  <form>
    <label class="checkbox-inline">
      <input type="checkbox"  value="1" name="state">State
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" value="2" name="district">District
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" value="3" name="Police_station">Police Station
    </label>
  </form>
</div>


<script>

$('[type="checkbox"]:gt(0)').on('click', () => $('[type="checkbox"]:eq(0)').prop('checked', $('[type="checkbox"]:gt(0)').is(':checked')));

</script>

//or

<script>
var $firstCb = $("input:checkbox:first");
var $otherCb = $("input:checkbox:not(:first)");

$otherCb.on("click",function(){
   var anyChecked = $otherCb.is(":checked");
   $firstCb.prop("checked",anyChecked);
});

</script>

Comments

Popular posts from this blog

datatable with both top and bottom scroll bar.

Display Loader and disable page while waiting for ajax request

Invalid bean property or is not readable to loading Jsp page