jeudi 7 mars 2019

Check if value of selected checkboxes are in data-attribute

I need to find out if the values of selected checkboxes can be found in the data-attribute of a given div.

Only if ALL the checkboxes with the values found in the data-attribute are checked, the result should be TRUE. If one or more checkboxes with values found in the data-attribute is unchecked, the result should be FALSE.

Examples:

<div id="towns" data-attribute="FRA, BER, HAM">...</div>

Selected Checkboxes: FRA, BER => Result: false

Selected Checkboxes: BER, HAM, MUC => Result: false

Selected Checkboxes: FRA, BER, HAM => Result: true

Selected Checkboxes: FRA, BER, HAM, MUC => Result: true

$("input[type=checkbox]:checked").each(function() {

  console.log($("#testdiv").data("town").split(",").indexOf($(this).val()) > -1);
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input type="checkbox" value="FRA" checked />
<input type="checkbox" value="BER" checked />
<input type="checkbox" value="HAM" />
<input type="checkbox" value="MUC" />

<div id="testdiv" data-town="FRA, BER, HAM">
  Lorem ipsum
</div>



Aucun commentaire:

Enregistrer un commentaire