Ilya Medvedev
Ilya Medvedev

Reputation: 1279

Event by clicking Checkbox

How do, when click a checkbox, in var check=''; will be added - okay, and if click again,okay will be removed.

How it make?

Upvotes: 1

Views: 92

Answers (3)

ipr101
ipr101

Reputation: 24236

var check;
$('#checkbox').change(function() {this.checked ? check = 'okay' : check = '';})

Upvotes: 0

James Allardice
James Allardice

Reputation: 166061

var check = "";
$("#yourcheckbox").change(function() {
    if($(this).is(":checked")) {
        check = "okay";
    }
    else {
        check = "";
    }
});

This does exactly what you ask, but the solution posted by @Rikudo is better if you can live without check having to contain "okay".

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 175098

var check = false;
console.log(check);
$('#checkbox').change(function() {
    check = $(this).is(':checked');
    console.log(check);
});

Will set the variable to true or false depending on the state.

Example.

Upvotes: 2

Related Questions