I have two HTML form fields in my application, one displays a dropdown for users to select if they are over 18 and the second field is for parent's name.
I want to make the parent_name field required only if the user is under 18. Here's my current code for the appropriate form parts.
<div>
<label for="over_18">Over 18?</label>
<select id="over_18" required multiple>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
<div class="form-group">
<label>Parent Name</label>
<input type="parent_name" id="parent_name" required>
</div>
I've created the following function so far, but I'm unable to understand how to use it.
function updateRequirements() {
var over_18 = document.getElementById('over_18').value;
if (over_18 = 'No') {
doccument.getElementById('parent_name').required = true;
} else {
doccument.getElementById('parent_name').required = false;
}
}
Update:
How to achieve the same result if the first input is of type date? So, now the second input is required only if the age provided in the first input is less than 18.