I’m trying to check if a checkbox has been selected so that if it is, I can then display another textbox, however I’ve tried the below code to try and check if the checkbox is checked or not but keep getting errors. I need to check before the form is submitted/posted as the text field that shows after needs submitting with the form.
HTML + PHP:
<div class="form-check">
<input type="checkbox" name = "job_mileage_check" class="form-check-input" id="job_mileage">
<label class="form-check-label" for="job_mileage">Mileage?</label>
</div>
<?php
$check_value = isset(['job_mileage_check']) ? 1 : 0;
?>
All/Any help is appreciated.
Advertisement
Answer
you can try this in front but with checkbox
Form
<div id='radios'> <div class="form-check"> <input type="radio" id="inputRadio1" value="data1"> <label class="form-check-label">myText1</label> </div> <div class="form-check"> <input type="radio" id="inputRadio2" value="data2"> <label class="form-check-label">myText2</label> </div>
JS
const inputRadio1 = document.getElementById('radioinputRadio1');
const inputRadio2 = document.getElementById('radioinputRadio2');
const btnSubmit = document.getElementById('btnSubmit');
btnSubmit.addEventListener('click', e => {
if (!inputRadio1.checked && !inputRadio2.checked) {
// error
// ...
e.preventDefault();
}
});