12

I use Bootstrap switch plugin to make my checkboxes looks like switch buttons. I need to check if the checkbox is checked in jQuery. I Googled a lot and I tried some advice and code snippets but without success.

I think the cleanest code I found is

$('#checkbox').attr('checked');

but it still wont work on my website. I have declared jQuery in head. I try to use this snippets on non-bootstrap-switch-checkboxes but still no success.

JS:

 <script>
    $(function(){
      $(".odeslat").click(function(){
           $('#pozadavky').slideUp(100);
           $('#datumpick').slideDown(300);

       var typpaliva = 'nic';
       var malpg = 'nic';¨

       /***Important condition***/

       if ($('#uho').attr('checked')) {
            $.cookie("typpaliva", "Diesel");
       }
       else {
            $.cookie("typpaliva", "Benzin");
       }


 /**********************************************/


    $.ajax({
   url: 'http://podivej.se/script_cas.php',
   data: {druh: '30'},
   type: "POST",
   success: function(data){
            alert($.cookie('typpaliva')); 
            alert($.cookie('malpg'));   
   }
   });

  });
});
</script> 

HTML

<input type="checkbox" name="testcheckbox" id="uho"/>
2
  • "it still wont work on my website" Please be more precise. What exactly does not work? What happens? What do you expect to happen? What have you done to debug the problem? Commented Jun 18, 2014 at 6:33
  • How do you trigger the switch ? Commented Jun 18, 2014 at 6:38

6 Answers 6

21

Use :

if ($('#uho').is(':checked')) {

instead of

if ($('#uho').attr('checked')) {
Sign up to request clarification or add additional context in comments.

2 Comments

@BhushanKawadkar How do you tell when the user checks or unchecks?
@pathros, you can register a .click(function().. or .change(function().. event handlers and then check $(this).is(':checked') which return true if checked and false if unchecked.
13

Demo

Use .prop() instead of .attr().

  • .prop returns - 'true' or 'false'. Use this
  • .is(':checked') returns - 'true' or 'false' -:checked is a pseudo slector. Or this
  • .attr returns - 'checked' or 'attribute undefined'.

$('#uho').prop('checked')

if($('#uho').prop('checked')){
    $.cookie("typpaliva", "Diesel");
}
else {
    $.cookie("typpaliva", "Benzin");
}

Comments

1

Based on a comment in the accepted answer, here's how I solved this for a Terms and Conditions checkbox in bootstrap:

HTML

<div class="checkbox">
   <label>
      <input id="agreeTAC" type="checkbox" value="">
      I have read and agree to the TERMS AND CONDITIONS listed on this page.
   </label>
</div>

Javascript (with jQuery)

$("#agreeTAC").change(function(){
    var agreed = $(this).is(':checked');
    console.log("agreeTAC value changed and is ", agreed);
    if(agreed === true) { 
       // do 'true' stuff, like enabling a 'Continue' button
    }
    else {
       // do 'false' stuff, like disabling a 'Continue' button
    }
})

Comments

0

$('#uho').attr('checked') will return undefined. you can use $('#uho:checkbox:checked').length property to check if checkbox is checked or not. Try this snippet:

 if ($('#uho:checkbox:checked').length > 0) {
        $.cookie("typpaliva", "Diesel");
   }
   else {
        $.cookie("typpaliva", "Benzin");
   }

Comments

0
  • use this code on page load

    document.getElementById("#uho").checked = true;

2 Comments

Please tell Why is that necessary while OP has jQuery ?
This is direct method. you can use java script for this purpose.
0

Use

  if($('#chkBoxID').is(":checked")){ // do stuff }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.