[RESOLVED]CheckBox checked

Does the line                 if ($(‘#CheckBox1′).prop(‘checked’, ‘true’)) {
                                        alert(‘CheckBox1 checked’);

actually cause the checkbox1 to be checked?

If so, how can I just check to see whether CheckBox1 has been checked?

      $(‘#CheckBox2′).click(function () {
            
              if ($(‘#CheckBox1′).prop(‘checked’, ‘true’)) {
                  alert(‘CheckBox1 checked’);
             }
             // $(‘#CheckBox1′).prop(‘checked’, ‘true’)
              alert(‘Hello2′);
              $(‘#TextBox1′).val("10");
              $(‘#DropDownList1′).val("20");
              $(‘input:radio[name=cars]:nth(0)’).attr(‘checked’, true);
              $(‘#TextBox1′).fadeOut(10000);
          });

davelewicki

Does the line                 if ($(‘#CheckBox1′).prop(‘checked’, ‘true’)) {

Yes it will cause the checkbox to be checked.

I guess you are trying to check if checkbox1 is checked or onclick of checkbox2, if so then you can use the below code to check that

 if ($('#CheckBox1').is(":checked")) {
                    alert('CheckBox1 checked');
                }

Complete Code

$('#CheckBox2').click(function () {

            //if ($('#CheckBox1').prop('checked', 'true')) {
            //Use the below code to check if checkbox 1 is selected ornot
            if ($('#CheckBox1').is(":checked")) {
                alert('CheckBox1 checked');
            }
            // $('#CheckBox1').prop('checked', 'true')
            alert('Hello2');
            $('#TextBox1').val("10");
            $('#DropDownList1').val("20");
            $('input:radio[name=cars]:nth(0)').attr('checked', true);
            $('#TextBox1').fadeOut(10000);
        });

EDIT:Added a demo

davelewicki

Does the line                 if ($(‘#CheckBox1′).prop(‘checked’, ‘true’)) {
                                        alert(‘CheckBox1 checked’);

actually cause the checkbox1 to be checked?

Yes. You can see a working example of this here.

davelewicki

If so, how can I just check to see whether CheckBox1 has been checked?

If you need to see if a particular element is checked, you can use the
is() function
available in jQuery :

// Use the is function along with the checked pseudoselector to determine if an element is checked or not
if($("#CheckBox1").is(':checked')){
        // Do something (it is checked)
}
else{
        // Do something else (it isn't checked)
}

You can see an example of this here. Additionally, you can still use the prop() approach that you tried earlier, however you would just need to set a single parameter and the function would return true
or false depending if the element was checked or not :

if($("#CheckBox1").prop('checked')){
        // Do something (it is checked)
}
else{
        // Do something else (it isn't checked)
}

Thanks!

Leave a Reply