Solved

How do I capture a checkbox change event using JQuery?

Posted on 2014-07-17
6
38,630 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
[X]
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
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 53

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

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.

Question has a verified solution.

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

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)

739 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