I have two inputs in the html and in JavaScript, I am trying to write a function that does a check for both inputs to show the button. The user needs to enter 7 text and check the box for the button to appear.
$(document).on("input", "#activationCode", function(e) {
e.preventDefault();
//Only enable button if the activation code is 7 characters
let checkbox = document.getElementById("checkbox");
if ($(this).val().length == 7 && checkbox.checked) {
$('.kit-activation-button').attr('disabled', false);
} else {
$('.kit-activation-button').attr('disabled', true);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="activationCode" name="code" class="form-control required" required aria-required="true" maxlength="7">
<input type="checkbox" id="checkbox" />
<button type="submit" class=" btn btn-primary" id="Button" disabled>submit</button>
I tried this but I still cant get the disable attribute to disappear when you enter 7 characters and check the box.
2
Answers
id="checkbox"
should beid="activation-pwn-check"
, andid="Button"
should beid="kit-activation-button"
.Move the code that enables the submit button into its own function. Then you can call it from event handlers for both the text input and checkbox.
You are not naming your class
.kit-activation-button
on your element, so you’re not calling anything. Also you are not checking the state of your checkbox on activation (checked unchecked)You can simplify this by making a function for the redundant code, but I felt long hand was a better explanation to show where the short-comings were.
A simplified version would be: