I am trying to simply create code that checks whether an checkbox input has been checked. I have tried both prop and is(':checked'). I don't understand why my class is not adding.
Does anyone see what is wrong?
if($('#checkText').is(':checked')) {
$('#notiPhone').addClass('active');
console.log('Text box should be showing');
}
else {
$('#notiPhone').removeClass('active');
console.log('Text box should NOT be showing');
}
#notiEmail, #notiPhone {
display: none;
transition: all .4s ease;
}
#notiEmail.active, #notiPhone.active {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="text" name="notiName" placeholder="Name">
<p>What type of notifications would you like to receive?</p>
<label>Text Notifications</label>
<input type="checkbox" name="checkText" id="checkText" value="text">
<label>Email Notifications</label>
<input type="checkbox" name="checkEmail" id="checkEmail" value="email">
<input type="email" name="notiEmail" id="notiEmail" placeholder="Email*">
<input type="tel" name="notiPhone" id="notiPhone" placeholder="Phone*">
</form>
$('#notiPhone').change(function() { ...})