Skip to main content
Convert code to runnable code snippet and minor condition update
Source Link
kabirbaidhya
  • 3.4k
  • 3
  • 37
  • 60

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function() {
  if (this.checked) {
    console.log('checked');
  } else {
    console.log('un-checked');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" id="customCheck1">
$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});
 

EDIT: notNot a nice programming expression if (boolean == true) though .checked property might return other type variables as well..

It is better to use .prop("checked") instead. It returns true and false only. Moreover, you avoid the bool == true expression.

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

EDIT: not a nice programming expression if (boolean == true) though .checked property might return other type variables as well..

It is better to use .prop("checked") instead. It returns true and false only. Moreover, you avoid the bool == true expression.

To act on a checkbox being checked or unchecked on click.

$('#customCheck1').click(function() {
  if (this.checked) {
    console.log('checked');
  } else {
    console.log('un-checked');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" id="customCheck1">
 

EDIT: Not a nice programming expression if (boolean == true) though .checked property might return other type variables as well..

It is better to use .prop("checked") instead. It returns true and false only.

clarify 'if (condition == true) is not a nice programming custom' coercion
Source Link
Jiří
  • 485
  • 7
  • 18

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

EDIT: not a nice programming expression if (boolean == true) though .checked property might return other type variables as well..

It is better to use .prop("checked") instead. It returns true and false only. Moreover, you avoid the bool == true expression.

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

EDIT: not a nice programming expression if (boolean == true) though .checked property might return other type variables as well..

It is better to use .prop("checked") instead. It returns true and false only. Moreover, you avoid the bool == true expression.

if (condition == true) is not a nice programming custom
Source Link
Jiří
  • 485
  • 7
  • 18

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked == true) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});

To act on a checkbox being checked or unchecked on click.

<input type="checkbox" id="customCheck1">

$('#customCheck1').click(function () {
  if (this.checked) {
    console.log('checked');
  }
  else {
    console.log('un-checked');
  }
});
deleted 45 characters in body
Source Link
ROOT
  • 11.6k
  • 5
  • 32
  • 48
Loading
Source Link
Dan Walters
  • 1.3k
  • 1
  • 18
  • 31
Loading
Post Made Community Wiki by Dan Walters