Skip to content
the poor coder

checkbox on click jquery

Poor Man
Published
Reading time
1 min read
On this page

Checkbox On Click jQuery#

If you want to perform some action when a checkbox is clicked, you can use jQuery to achieve that. Here is how you can do it:

HTML Code#

Text

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

In the above code, we have created a checkbox with an id "myCheckbox".

jQuery Code#

Text

$('#myCheckbox').click(function() {
  if ($(this).is(':checked')) {
    // Checkbox is checked
    // Perform some action here
  } else {
    // Checkbox is not checked
    // Perform some action here
  }
});

In the above code, we have attached a click event handler to the checkbox with id "myCheckbox". When the checkbox is clicked, the function inside the click event handler is called. We are checking if the checkbox is checked or not using the is(':checked') function. If the checkbox is checked, we can perform some action, and if it is not checked, we can perform some other action.

Alternative Method#

Another way to achieve the same result is by using the change() function instead of the click() function in jQuery. Here is how you can do it:

Text

$('#myCheckbox').change(function() {
  if ($(this).is(':checked')) {
    // Checkbox is checked
    // Perform some action here
  } else {
    // Checkbox is not checked
    // Perform some action here
  }
});

In the above code, we have attached a change event handler to the checkbox with id "myCheckbox". When the checkbox is changed, the function inside the change event handler is called. We are checking if the checkbox is checked or not using the is(':checked') function. If the checkbox is checked, we can perform some action, and if it is not checked, we can perform some other action.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC