2

I have a html checkbox in my html form. Now, i want to assign 1 when it is checked and assign 0 when it is unchecked. So, how can i do that with jquery or java Script?

<!DOCTYPE html>
<html>

  <script language="javascript" type="text/javascript">
  <!--
  function greeter() {
      alert(document.getElementById(vehicleChkBox).value);
  }
  $('#vehicleChkBox').change(function(){
       if($(this).attr('checked')){
            $(this).val(1);
       }else{
            $(this).val(0);
       }
  });
  </script>
  <body>
    <form>
      <input type="checkbox" name="vehicle" id="vehicleChkBox" value="FALSE" />
      <input type="submit" onsubmit="greeter()">
    </form>
  </body>

I tried with this. but, does not work.

2
  • Please post ur code and what you have tried so far! Commented Sep 29, 2014 at 11:27
  • Assign to value of checkbox. Commented Sep 29, 2014 at 11:31

6 Answers 6

4

JS:

var checkbox = document.querySelector("input[type='checkbox']");
  checkbox.addEventListener("change",function(){
     this.value = this.checked ? 1 : 0;
  });

jQuery:

$(":checkbox").change(function(){
     $(this).val($(this).is(":checked") ? 1 : 0);
});
Sign up to request clarification or add additional context in comments.

Comments

1

You may use JQuery for this with change event:

$(function() {
  $('#vehicleChkBox').on('change', function(e) {
    e.stopPropagation();
    this.value = this.checked ? 1 : 0;
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle" id="vehicleChkBox" value="0" />

Comments

0

You simply need to put its value as 1. This way when it is checked it will return 1 and when it is not checked it will return nothing so that way you can do a check and assign zero.

<input type="checkbox" name="my_checkbox" value="1" />

4 Comments

Where does the dynamic value come from??
when we check assign 1 and uncheck assign 0.
That is what this does. I think you need to get more familiar with how forms work.
is it changing when we check and uncheck?
0

I am using this and this is working absolutely fine:

$("#checkkBoxId").attr("checked") ? alert("Checked") : alert("Unchecked");

Note: If the checkbox is checked it will return true otherwise undefined, so better check for the "TRUE" value.

OR

if(!$("#checkkBoxId").is(':checked') ){
 $(this).val('1');
}
else
$(this).val('0');

Comments

0
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script> 
   $(document).ready(function() {
         $('#vehicleChkBox').click(function(){
             if($(this).is(':checked'))
             {              
                  $(this).val('1');
             }
             else
             {
                 $(this).val('0');
             }
         });
    });
</script>

Comments

0

This will work fine for setting value

   $("#checkkBoxId").prop("checked") ? ($("#checkkBoxId").val(1)) :    ($("#checkkBoxId").val(0));

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.