4

I am trying to do some checkbox manipulation with jquery. I have a modal popup and checkbox in it and I am using jquery to manipulate the checked mode. There is a variable which has either True or False, so if its True then check otherwise uncheck. But, in my case even when the value is False, the checkbox still stays checked. This is the code I am using:

$(document).on("click", ".open-EditCC", function () {            
            var life = $(this).data('life');           
            $('#<%=chkLife.ClientID%>').attr('checked', life);
            $('#editCC').modal('show');
          });

life variable gets either True or False but all the time the checkbox is checked, I set a breakpoint I can see that the value is False. Any idea what am I doing wrong? Thanks in advance, Laziale

2 Answers 2

4

The value of the checked attribute is "checked" or the checked attribute is not present so use this:

// This is assuming life has the string value of "True" or "False"
// if it's a boolean change to if (life)
if (life === 'True') {
    $('#<%=chkLife.ClientID%>').attr('checked', 'checked');
} else {
    $('#<%=chkLife.ClientID%>').removeAttr('checked');
}
Sign up to request clarification or add additional context in comments.

Comments

0

I guess it should be enough to remove attribute when neccessary.

if(life){
 $('#<%=chkLife.ClientID%>').attr('checked', 'checked');
}
else

{
$('#<%=chkLife.ClientID%>').removeRttr('checked');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.