You are viewing limited content. For full access, please sign in.

Question

Question

Setting a Check Box Only Works Once

asked on September 22, 2017 Show version history

I have this code that will set and disable the QA Project Support checkbox if Specification = Yes:

$(document).ready(function(){

//check the QAE box and disable if specification = yes
  $('.specification input').change(function () {
    if ($(this).val() == "Yes")
    {
      $("#Field104-2").prop("disabled", false); //Enable so it can be changed
      $('#Field104-2').attr('checked', true).change(); //QAE is Not Required
      $("#Field104-2").prop("disabled", true); //Disable so it cannot be changed
    }
    else
    {
      $("#Field104-2").prop("disabled", false); //Enable so it cannot be changed
    }
  });
  $(".specification input:checked").each(function(){  
    $(this).trigger('change');
  });

});

The first time I use it, works great:

Then the user decides to say NO. It doesn't uncheck but it does enable it for the user to decide. Again ... still working great!

It allows me to uncheck it .... again, working as designed!

Here's the issue. When the user decides to change Specification = Yes again, it disables, but it doesn't set the checkbox to checked:

Any ideas? Thanks in advanced!

0 0

Answer

SELECTED ANSWER
replied on September 23, 2017 Show version history

The problem is in line 8 of your Javascript: 

$('#Field104-2').attr('checked', true).change(); //QAE is Not Required

Setting the checkbox's checked attribute is how you would set its initial checked status of true or false.  But it is the initial value only.  Since it is the initial value, that's why it only works the first time, the actual user action of unchecking the box has no impact on the attribute, it continues to stay true.

What you need to be setting is the checkbox's checked property, which is the current checked status of true or false.

You are probably thinking that the difference between these two feels very aggravating and annoying.  And you would be correct, it is...  Welcome to the world of coding wink .

Try this line of code instead: 

$('#Field104-2').prop('checked', true).change(); //QAE is Not Required
1 0

Replies

replied on September 23, 2017

OMG ... you are so right about frustration! Some day I'll get it! Thank you, it worked.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.