Solved

How do I capture a checkbox change event using JQuery?

Posted on 2014-07-17
6
33,504 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 50 total points
Comment Utility
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
Comment Utility
Thanks Guys. I will try these out tomorrow, and then dole out the points.
0
 

Expert Comment

by:Umesh Shejole
Comment Utility
Just tried working properly.

Thank you !!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now