Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox change event not firing

This is my code:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    $('.chk').live('change', function() {
      alert('change event fired');
    });
    $('a').click(function() {
      $('.chk').attr('checked', true);
    });
  });
</script>
</head>
<body>
<div class="chkHolder">
  <input type="checkbox" class="chk" name="a" id="a" />
  <input type="checkbox" class="chk" name="b" id="b" />
</div>
<a href="#">check all</a>
</body>
</html>

When I click on the "check all" hyperlink, I want the change event fired for each of the checkboxes. However, that is not happening.

Any ideas?

Many thanks!

like image 591
user356247 Avatar asked Sep 12 '11 12:09

user356247


1 Answers

use: $('.chk').attr('checked', true).change();

Live example: http://jsfiddle.net/NRPSA/1/

like image 160
Samich Avatar answered Oct 05 '22 23:10

Samich