Checkbox checkbox checked / unchecked

I am trying to do some jquery manipulation. I have a modal popup and checkbox in it, and I use jquery to control checked mode. There is a variable that has either True or False, so if its True then check the box otherwise. But in my case, even if the value is False, the checkbox still remains checked. This is the code I'm using:

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

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

+4
source share
2 answers

The checked attribute value is "checked" or the checked attribute is missing, so use this:

 // This is assuming life has the string value of "True" or "False" // if it a boolean change to if (life) if (life === 'True') { $('#<%=chkLife.ClientID%>').attr('checked', 'checked'); } else { $('#<%=chkLife.ClientID%>').removeAttr('checked'); } 
+4
source

I think this should be enough to remove the attribute if necessary.

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

All Articles