1

I have this switcher (Checked):

<div class="switch try">
<label><input checked="" type="checkbox" class="checkit"><span class="lever switch-col-green"></span></label>
</div>

How to know if this one is checked or unchecked when a user click on it ?

Here what i tried:

$(document).on('click','.try',function(e){
                        if($(".checkit").is(':checked')) {
                            alert("checked");
                        } else {
                            alert("Not checked");
                        }
});

But i don't get it working !

Jis Maxi
  • 226
  • 1
  • 4
  • 16
  • 2
    Possible duplicate of [Check if checkbox is checked with jQuery](http://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery) – Daniel Corzo Nov 30 '16 at 20:31
  • You should most likely attach the `change` event to the checkbox instead, and use a label, not a div. – adeneo Nov 30 '16 at 20:34
  • If you're trying to show an alert when you click the checkbox container use `$('.try').on('click', ...)` instead of `$(document).on('click', '.try', ...)`. But also be aware that if you click the checkbox, the `click` event will be propagated from the checkbox to the div element. Could you also add some explanation about what are you trying to achieve with that code? – Santiago Rojo Nov 30 '16 at 20:43
  • @adeneo i solved the problem by changing click event to change event, many thank's. – Jis Maxi Nov 30 '16 at 20:47

2 Answers2

0

Try to use this:

$(".checkit:checked").length > 0;

or

$(".checkit").is(":checked")
P.S.
  • 15,970
  • 14
  • 62
  • 86
0

You need to change your html from checked="" -> checked

<input checked type="checkbox" class="checkit">

Jquery is correct as is

av192
  • 444
  • 1
  • 4
  • 10