2
I have a checkbox option that when active, shows a second check. I need to ensure that the second checkbox is only active if the first one is active. My function shows and hides the second check according to the value of the first, but I need it to also uncheck the second one when the first one is unchecked and this is not working. Can you help me ? Follow my code.
$("#ck1").click(function() {
if ($("#ck1").is(':checked')) {
$("#ck2").show();
} else {
if ($("#ck2").is(':checked')) {
$("#ck2").prop('checked', false);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="checkbox" id="ck1" name="ck1" />
<label for="ck1">Check 1</label>
</div>
<div style="display: none;">
<input type="checkbox" id="ck2" name="ck2" />
<label for="ck2">Check 2</label>
</div>
Thanks for the answer. Hide the second checkbox works, just uncheck it with the
prop
that doesn’t happen.– goldenleticia
@goldenleticia Interestingly, it doesn’t work here. It works normal here on my PC and also on Jsfiddle: https://jsfiddle.net/vw3yqLh6/
– Sam
@goldenleticia There must be a bug in the Stack snippet.
– Sam
but in jsfiddle is not
prop
, isattr
– goldenleticia
@goldenleticia sorry, I forgot to change: https://jsfiddle.net/vw3yqLh6/1/
– Sam