The code is below and checkbox is always FALSE. But on page it is different. It doesn't get checked or unchecked.
<script type="text/javascript">
$('.cbOzelHastaAdi').unbind('click');
$('.cbOzelHastaAdi').click( function() {
var parentDiv = $(this).parent().get(0);
var cbs = $(parentDiv).find('table input:checkbox');
if($(this).attr("checked") === "true") {
cbs.each(function() { $(this).attr('checked', false); });
}
else{
cbs.each(function() { $(this).attr('checked', true); });
}
})
</script>
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
When using jQuery and you wish to read whether a checkbox is checked or not. $('#checkboxid').is(':checked'); This will return true if the checkbox is checked and false if left unchecked.
The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
One of the problems was that you had the checked
attribute on the span surrounding the top checkbox and label and were binding an event handler to the span, therefore checked
will always remain checked
on the span.
I have moved the event handlers to the checkbox instead and tidied up the code a bit. Whilst I don't believe there is a problem in constructing your own attributes on HTML elements i.e. a checked attribute on a span element (I think XHTML strict validation fails with them), I don't whether it's considered good practice to do so.
I can see that you are using ASP.NET, based on the ID mangling - you can use server side <%= myControl.ClientID %>
to get the mangled id to render in the HTML sent to the client.
Working Example here
$(function() {
$('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi').unbind('click');
$('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi').click( function() {
var cbs = $('table input:checkbox');
if($(this).is(':checked')){
cbs.each(function() { $(this).attr('checked', true); });
}
else{
cbs.each(function() { $(this).attr('checked', false); });
}
});
});
EDIT:
In response to your comment, you have a couple of options for resolving the clientid. If you write your jQuery in the aspx page, then you can simply use
$('#<%= cbOzelKurumHastasi.ClientID %>')
in place of
$('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi')
If you have your jQuery in an external script file then you could put this in your aspx page
<script type="text/javascript">
var cbOzelKurumHastasi = '#<%= cbOzelKurumHastasi.ClientID %>';
</script>
and then use the variable in your external script file
$(function() {
$(cbOzelKurumHastasi).unbind('click');
$(cbOzelKurumHastasi).click( function() { ...
For other options take a look at this question and answer - How to stop ASP.NET from changing ids in order to use jQuery
I usually us the .is() functionality of jQuery to check for this
$('.cbOzelHastaAdi').click( function() {
if($(this).is(':checked')){
...
}else{
...
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With