Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  70] [ 4]  / answers: 1 / hits: 38995  / 14 Years ago, wed, february 2, 2011, 12:00:00

I have two functions.



The first function translates a div click into a checked/unchecked toggle.
The second function translates a checkbox change into a hide/show event.



The problem is that when I use the first function to check/uncheck the box, the second function is not called. I am new to javascript, thanks.



<script type=text/javascript>
$(document).ready(function() {
$(:checkbox).parent().click(function(evt) {
if (evt.target.type !== 'checkbox') {
var $checkbox = $(:checkbox, this);
$checkbox.attr('checked', !$checkbox.attr('checked'));
evt.stopPropagation();
return false;
}
});
});
</script>

<script type=text/javascript>
$(document).ready(function() {
$(:checkbox).change(function() {
if($(this).attr(checked)) {
$('.'+this.id).show();
}
else {
$('.'+this.id).hide();
}
});
});
</script>

More From » jquery

 Answers
57

The change event does not fire when you programmatically change the value of a check box. What you can do to ensure it fires is:



 $(:checkbox).parent().click(function(evt) {
if (evt.target.type !== 'checkbox') {
var $checkbox = $(:checkbox, this);
$checkbox.attr('checked', !$checkbox.attr('checked'));
$checkbox.change();
}
});

[#93945] Monday, January 31, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;