I have a field with a drop down list in my html form. It contains two possibilities, I would like to make the following field (first block or second block) appear or disappear depending on the selection, I found for radio buttons but not for a drop-down list… Can you help me do this in javascript?
Thanks a lot.
Code html :
<div class="col-md-2">
<select class="form-select text-secondary mt-4" id="autoSizingSelect" name="service">
<option value="midi" data-id="#midi">Midi</option>
<option value="soir" data-id="#soir">Soir</option>
</select>
</div>
// First block
<p class="text-secondary text-center mt-5">Heure midi</p>
<div class="col-md-4">
<select class="form-select text-secondary" id="midi" name="heure_midi">
<option value=""></option>
<option value=" 12:00">12:00</option>
<option value="12:15">12:15</option>
<option value="12:30">12:30</option>
<option value="12:45">12:45</option>
<option value="13:00">13:00</option>
<option value="13:15">13:15</option>
<option value="13:30">13:30</option>
<option value="13:45">13:45</option>
<option value="14:00">14:00</option>
</select>
</div>
//Second block
<p class="text-secondary text-center mt-4">Heure soir</p>
<div class="col-md-4">
<select class="form-select text-secondary" id="soir" name="heure_soir">
<option value=""></option>
<option value="19:00">19:00</option>
<option value="19:15">19:15</option>
<option value="19:30">19:30</option>
<option value="19:45">19:45</option>
<option value="19:00">20:00</option>
<option value="20:15">20:15</option>
<option value="20:30">20:30</option>
<option value="20:45">20:45</option>
<option value="21:00">21:00</option>
</select>
</div>
2
Answers
You can combine
document.querySelectorAll
andclassList.toggle
triggered with theonchange
event of theselect
to achieve this, with a predefined.hidden
class.Please see snippet exemple.
sorry but I’m desperate to see such complicated code to handle a form