mercredi 17 août 2016

How to get multiple checkbox toggle switches values when on change event via jquery

I have implemented toggle switch based on checkbox in my website. When I use one toggle switch I can able to get the value of the checkbox which I click. But I when I implement two or more toggle switches I cannot get the value of checkbox which click. It always returning the value of first the checkbox.

You can understand what I'm saying by clicking second toggle switch that I created on the following fiddle.

http://ift.tt/2beJqda

This is my html code:

<div class="switch" id="switch">
  <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox" checked data-value="one" >
  <label for="cmn-toggle-1"></label>
</div>

<br>

<div class="switch" id="switch">
  <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox" data-value="two">
  <label for="cmn-toggle-1"></label>
</div>

This is my jQuery code:

$(document).on("change", ".switch", function () {
    alert($(this).find('input').data('value'));
});




Aucun commentaire:

Enregistrer un commentaire