How can I change the value of the checkbox if checked?


or

I want to change the value of checkboxes when I select them from 0-1

This is what I tried

HTML

        <input type="checkbox" name="Semi Annual" id="1" value="0">
        Semi Annual <br>


        <input type="checkbox" name="Quaterly" id="2" value="0">
        Quaterly <br>


        <input type="checkbox" name="Monthly" id="3" value="0">
        Monthly <br>

JS

    $('input[type="checkbox"]').change(function(){
                 this.value = 1;
    });

Demo : http://codepen.io/evoque2015/pen/RNpOLq _

Wildavies

The jsfiddle below does what you want by switching the values ​​from 0 to 1 (or vice versa) depending on whether they are checked or not.

There are log demos for each loop, you don't actually need to do this. By default, oncheck's .checked property is already set to true or false, so you only need to use this property.

HTML

<input type="checkbox" name="Semi Annual" id="1" value="0">
        Semi Annual <br>
<input type="checkbox" name="Quaterly" id="2" value="0">
        Quaterly <br>
<input type="checkbox" name="Monthly" id="3" value="0">
        Monthly <br>

JS

$('input[type="checkbox"]').change(function(){

    // Changes value to 1 or 0 depending on whether it is checked or not
    if(this.value == 0){
        this.value = 1;
    }
    else{
        this.value = 0;  
    }

    // Log shows the value of each checkbox and also that the .checked property does the same thing by default
    $('input[type="checkbox"]').each(function(index, element){
        if(element.checked){
            console.log(this.name + ' has value of ' + this.value);
            console.log(this.name + ' is checked');   
        }
        else{
            console.log(this.name + ' has value of ' + this.value);
            console.log(this.name + ' is not checked');             
        }
    });
});

Related


How can I change the value of the checkbox if checked?

or I want to change the value of checkboxes when I select them from 0-1 This is what I tried HTML <input type="checkbox" name="Semi Annual" id="1" value="0"> Semi Annual <br> <input type="checkbox" name="Quaterly" id="2" value="0">

How can I change the boolean value if the checkbox is checked?

gmark11 I want to change a boolean value in a component file with a simple checkbox. It should remain false if not checked, otherwise it should be true. Element: export class UsersComponent { public Personal: boolean = false; } HTML: <label> <input type

How can I change the checked state to uncheck this Checkbox case?

Oscar Rojas Murillo Please help me here...I have a list of checkboxes with "isChecked" value in DB, but I don't know how to change this value when the checkbox is pressed. (For example, if I have a checkbox with isChecked value of "false", when I want to press

How can I change the checked state to uncheck this Checkbox case?

Oscar Rojas Murillo Please help me here...I have a list of checkboxes with "isChecked" value in DB, but I don't know how to change this value when the checkbox is pressed. (For example, if I have a checkbox with isChecked value of "false", when I want to press

How can I change the checked state to uncheck this Checkbox case?

Oscar Rojas Murillo Please help me here...I have a list of checkboxes with "isChecked" value in DB, but I don't know how to change this value when the checkbox is pressed. (For example, if I have a checkbox with isChecked value of "false", when I want to press

How can i change the class on :checked div containing checkbox

Casimir I want to disable the :hovereffect on the div that includes my checkbox :checked. I tried: input[type=checkbox]:checked ~ #mydiv:hover {my:css} It only works on div/class that doesn't contain my checkbox. How can I do this? Here is my code: http://code

How can I change the checked state to uncheck this Checkbox case?

Oscar Rojas Murillo Please help me here...I have a list of checkboxes with "isChecked" value in DB, but I don't know how to change this value when the checkbox is pressed. (For example, if I have a checkbox with isChecked value of "false", when I want to press

How can I change the checked state to uncheck this Checkbox case?

Oscar Rojas Murillo Please help me here...I have a list of checkboxes with "isChecked" value in DB, but I don't know how to change this value when the checkbox is pressed. (For example, if I have a checkbox with isChecked value of "false", when I want to press