[ACCEPTED]-Checkbox and JQUERY: "is checked" always FALSE-checkbox

Accepted answer
Score: 39

One of the problems was that you had the 24 checked attribute on the span surrounding the top 23 checkbox and label and were binding an event 22 handler to the span, therefore checked will always 21 remain checked on the span.

I have moved the event 20 handlers to the checkbox instead and tidied 19 up the code a bit. Whilst I don't believe 18 there is a problem in constructing your 17 own attributes on HTML elements i.e. a checked 16 attribute on a span element (I think XHTML 15 strict validation fails with them), I don't 14 whether it's considered good practice to 13 do so.

I can see that you are using ASP.NET, based 12 on the ID mangling - you can use server 11 side <%= myControl.ClientID %> to get the mangled id to render in 10 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 9 to your comment, you have a couple of options 8 for resolving the clientid. If you write 7 your jQuery in the aspx page, then you can 6 simply use

$('#<%= cbOzelKurumHastasi.ClientID %>')

in place of

$('#rptOzel_ctl00_rptOzelHastalar_ctl00_cbOzelKurumHastasi')

If you have your 5 jQuery in an external script file then you 4 could put this in your aspx page

<script type="text/javascript">
var cbOzelKurumHastasi = '#<%= cbOzelKurumHastasi.ClientID %>';
</script>

and then 3 use the variable in your external script 2 file

$(function() {     
            $(cbOzelKurumHastasi).unbind('click');
            $(cbOzelKurumHastasi).click( function() { ...

For other options take a look at this 1 question and answer - How to stop ASP.NET from changing ids in order to use jQuery

Score: 7

I usually us the .is() functionality of 1 jQuery to check for this

$('.cbOzelHastaAdi').click( function() {
  if($(this).is(':checked')){
    ...
  }else{
    ...
  }
})
Score: 3

could it be because you are comparing the 3 value of the checked property to a string 2 rather than a boolean and using the === operator 1 which compares both value and type?

Score: 1

I came across the same problem ones; checking 10 whether a checkbox is checked ( in any of 9 the different ways) always returned false.

My 8 solution (course of problem) was in the 7 JQuery selector that was used.

uzay95 used 6 the class selector ".", to select its checkboxes.

$('.cbOzelHastaAdi')**

Which 5 returns a result set. Therefor, checking 4 if it is checked is invalid, because their 3 might be multiple checkboxes... I know he's 2 using the this keyword in his code, but for 1 me this also did not fix the problem.

As soon as you select the checkbox by ID, you can evaluate its 'checked' attribute.

Score: 0

I've found that the following is probably 4 the best way to determine checked status 3 in jQuery:

var cbs = $(parentDiv).find('table input:checkbox:checked');

This will give you an array of 2 each input that has been checked.

This is 1 mostly from the jQuery docs:

Selectors/Checked

Score: 0

Russ Cam's answer works, but why not jQueryify 7 it some more?

$(function() {     
    $('.cbOzelHastaAdi').live('click', function() {
        $(this).parents('div').find('table input:checkbox').attr('checked', $(this).attr('checked'));
    });
});

This assumes that the class 6 cbOzelHastaAdi is now attached to the checkbox instead 5 of the span element. This should allow 4 you to avoid all of the messy ASP renaming 3 and allow multiple similar tables per page 2 without the need for multiple click event 1 functions.

Score: 0

This is also working:

(($('input[name="myCheckBox"]:checked').val())=='on')

It will return either 2 true or false

where the checkbox is defined 1 as:

<input type="checkbox" name="myCheckBox">

More Related questions