Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I capture a checkbox change event using JQuery?

Posted on 2014-07-17
6
36,684 Views
Last Modified: 2016-09-19
I have a web page with several check boxes on it. When a checkbox is checked, I need to perform some functionality that I won't go into.
Anyway, so my question is "How do I capture the Check Box Change event" using JQuery?

I tried using the following code below, however the checkbox event only fires sporadically, and not on every click
event like I need it to.


$(document).ready(function ()
{
   $(":checkbox").change(function ()
      {
       alert('I have been checked');
      }

});
});
0
Comment
Question by:brgdotnet
6 Comments
 
LVL 4

Accepted Solution

by:
Jack Leach earned 280 total points
ID: 40203718
bind to the click() event instead:

$(document).ready(function() {

 $("#yourcheckbox").on("click", function() {
    alert($(this).attr("checked"));
  });

});

hth
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40203729
Hi Jack,

The issue here is that my page has over 50 check boxes on it. So I think that $("#yourcheckbox"). is too specific to the id of the check box control isn't it.

Basically I just want to know when any checkbox has been clicked.
0
 
LVL 4

Expert Comment

by:Jack Leach
ID: 40203737
Sorry... the $(":checkbox") syntax should be fine, just use the click event instead of the change event.  Change seems to be problematic with checkboxes for whatever reason

Alternatively, try the following syntax:

$("[type=checkbox]").click(function() {
  // your stuff
});
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 50 total points
ID: 40203772
Here is a working sample I made http://jsbin.com/josoq/1/edit?html,output
Notice no pop up if not checked.

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  <meta charset="utf-8">
  <script>
    $(function(){
  $('input[type="checkbox"]').change(function(){
    var id=$(this).attr('id');
    var something = $(this).attr('data-something');
    var value=$(this).val();
    if ($(this).prop('checked')){
        // do something
        alert('id:'+id+' something:'+something+' value:'+value);
        }
  });
  
});
    </script>
  <title>padas</title>
</head>
<body>
<input id="1" type="checkbox" data-something="1 html" name="interest" value="html">HTML<br>
<input id="2" type="checkbox" data-something="2 css"  name="interest" value="css">CSS<br>
<input id="3" type="checkbox" data-something="3 js"   name="interest" value="js">Javascript<br>    
<input id="4" type="checkbox" data-something="4 xml"  name="interest" value="xml">XML<br>
<input id="5" type="checkbox" data-something="5 asp"  name="interest" value="asp">ASP<br>
<input id="6" type="checkbox" data-something="6 php"  name="interest" value="php">PHP<br>    
  
</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40203798
Thanks Guys. I will try these out tomorrow, and then dole out the points.
0
 

Expert Comment

by:Umesh Shejole
ID: 41805858
Just tried working properly.

Thank you !!!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question