vendredi 27 mars 2015

jQuery toggle multiple checkboxes with one button

i am trying to code an easy way to check and uncheck form checkboxes with jQuery. I have tried some of the examples found in stackoverflow, but my limited JS knowledge is keeping me from getting results.


Here is my code, and it is not changing the input checkboxes to "checked" as i intended, i also included a button to clear the selection, but JS still does not reference it.


HTML



<section id="opzioni">

<div class="bottoni-toggle">
<!-- toggle button -->
<input id="#select-all" type="button" value="Tutti" title="Toggle">
<!-- clear button -->
<input id="#select-none" type="button" value="Nessuno" title="Clear">
</div>

<div class="col_opt">
<span class="custom_chb_wrapper">
<span class="custom_chb">
<input class="bottone-opzioni" id="_4wd" name="_4wd" type="checkbox">
<input id="_4wd" name="_4wd" type="hidden" value="0">
</span>
<label>4WD</label>
</span>
</div>

<div class="col_opt">
<span class="custom_chb_wrapper">
<span class="custom_chb">
<input class="bottone-opzioni" id="_airbag" name="_airbag" type="checkbox">
<input id="_airbag" name="_airbag" type="hidden" value="0">
</span>
<label>Airbag</label>
</span>
</div>


jQuery



$(document).ready(function() {
$("#select-all").click(function() {
var checkBoxes = $("input.bottone-opzioni");
checkBoxes.prop("checked", !checkBoxes.prop("checked"));
});
});


Here is the fiddle i set up to test: Hope you can help.





Aucun commentaire:

Enregistrer un commentaire