Skip to content
Fix Code Error

jQuery checkbox checked state changed event

March 13, 2021 by Code Error
Posted By: Anonymous

I want an event to fire client side when a checkbox is checked / unchecked:

$('.checkbox').click(function() {
  if ($(this).is(':checked')) {
    // Do stuff
  }
});

Basically I want it to happen for every checkbox on the page. Is this method of firing on the click and checking the state ok?

I’m thinking there must be a cleaner jQuery way. Anyone know a solution?

Solution

Bind to the change event instead of click. However, you will probably still need to check whether or not the checkbox is checked:

$(".checkbox").change(function() {
    if(this.checked) {
        //Do stuff
    }
});

The main benefit of binding to the change event over the click event is that not all clicks on a checkbox will cause it to change state. If you only want to capture events that cause the checkbox to change state, you want the aptly-named change event. Redacted in comments

Also note that I’ve used this.checked instead of wrapping the element in a jQuery object and using jQuery methods, simply because it’s shorter and faster to access the property of the DOM element directly.

Edit (see comments)

To get all checkboxes you have a couple of options. You can use the :checkbox pseudo-selector:

$(":checkbox")

Or you could use an attribute equals selector:

$("input[type='checkbox']")
Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Trying to append a Polymer element in another…
  • jQuery Mobile: document ready vs. page events
  • Adding Max to a Result After Group By
  • Apache server keeps crashing, "caught SIGTERM,…
  • "Thinking in AngularJS" if I have a jQuery background?
  • javax.faces.application.ViewExpiredException: View…
  • Understanding checked vs unchecked exceptions in Java
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Polymer trigger function in child element using…
  • Kodein + Ktor = mutation attempt of frozen…
  • ember.js, ember-cli: Outlets not nesting properly
  • Vuex dynamic checkboxes binding
  • Smart way to truncate long strings
  • Checkbox Check Event Listener
  • configure: error: C compiler cannot create executables
  • Trouble using dispatchEvent using Polymer and Firefox
  • How to use java.net.URLConnection to fire and handle…
  • Accessibility and all these JavaScript frameworks
  • Customize Bootstrap checkboxes
  • Javascript to check whether a checkbox is being…
  • How to reset after dynamically loading data using in…
  • The definitive guide to form-based website authentication
  • Why do boxes become checked and then unchecked after…
  • React-router urls don't work when refreshing or…
  • Vuetify v-tabs v-tab-item overflows window width
  • TLS 1.3 server socket with Java 11 and self-signed…
  • MQTT paho - no error when failed to publish message
  • au build complains about file not found or accessible
  • Why is "throws Exception" necessary when calling a function?
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • Add polymer-dart element to div
  • Check if checkbox is NOT checked on click - jQuery
  • After a little scroll, the sticky navbar just is not…
  • Triggering text area by clicking related select/checkbox
  • Does it make sense to integrate backbone.js with ASPNET MVC?
  • An Authentication object was not found in the…
  • How to check file MIME type with javascript before upload?
  • Vuetify: checkbox shows status is checked when it is…
  • Paper checkbox and -webkit-column-count
  • SPA best practices for authentication and session management
  • Ember.js - Using a Handlebars helper to detect that…
  • Do sessions really violate RESTfulness?
  • Java HTTPS client certificate authentication
  • How can I determine whether a 2D Point is within a Polygon?
  • Nuxt.js error: The client-side rendered virtual DOM…
  • VueJS Master Checkbox Toggle with array values
  • Add attribute 'checked' on click jquery
  • Why do git fetch origin and git fetch : behave differently?
  • Best approach to real time http streaming to HTML5…
  • NextJS - can't identify where state change is…
  • Single Page Application: advantages and disadvantages
  • find all unchecked checkbox in jquery
  • How to make the checkbox unchecked by default always
  • C# Net.Core Object.Equals() returning false even if…
  • Save several Backbone models at once
  • Sending data to TCPServer more than one time
  • Eclipse will not start and I haven't changed anything
  • .prop() vs .attr()
  • Ubuntu apt-get unable to fetch packages
  • ASP.NET Custom Validator Client side & Server…
  • How to push objects in AngularJS between ngRepeat arrays
  • SVG Mask is "bleeding" on canvas edges
  • Fastest way to iterate over all the chars in a String
  • What values for checked and selected are false?
  • Next.js Redirect from / to another page
  • How to access this variable from my store / state in…
  • Android APK signatures V1 and V2 conflict
  • Backbone.js frontend with RESTful Rails backend?
  • Why is it common to put CSRF prevention tokens in cookies?
  • Server Client send/receive simple text
  • NEXT.JS with persist redux showing unexpected behaviour
  • IndexError: list index out of range error on python
  • How to change the background color on a input…
  • use React or Vue over server side template engines
  • Ukkonen's suffix tree algorithm in plain English
  • Best strategy when dealing with a react-query…
  • Passing a function as a parameter to child component…
  • Latest jQuery version on Google's CDN
  • How to handle the authentication token and and…
  • Managing shared/singleton client objects in React
  • Start redis-server with config file
  • What should main() return in C and C++?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Java SSLHandshakeException "no cipher suites in common"
  • How to divide my app into reusable components using…
  • "Error: Main method not found in class MyClass,…
  • Flutter - Cubit - loaded state - managing…
  • problem with client server unix domain stream…
  • Use foreach on ajax POST data received with comma…
  • How to use local storage to access combined reducer…
  • C++ template,typename and operator
  • GraphQL resolver context working in Playground but…
  • Polymer 1.0: How to pass an event to a child-node…
  • Backbone Marionette modules as Widgets similar to…
  • how to bind svelte dynamic components values
  • Click toggle with jQuery
  • What's the proper value for a checked attribute of…
  • Playing HTML5 video on fullscreen in android webview
  • AttributeError: 'tuple' object has no attribute

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to sort an ArrayList?

Next Post:

How to create a dialog with “yes” and “no” options?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error