I'm trying for the first time to work with forms in HTML. But sadly, I have a problem that I'm unable to solve. I want to show specific form-rows at a specifically selected option. I can change the style to display: block, but a few form-rows are permanently displayed and I can change it to display: none to hide it.
Here a Video preview of the problem: https://www.youtube.com/watch?v=WmEKfAOR7A8
Or test it live here: https://bl4cklist.de/contact
My JS code for the display Thing:
function yesnoCheck() {
// Allgemeine Fragen
if (document.getElementById("yesCheck").selected) {
document.getElementById("ifYes").style.display = "block";
document.getElementById("ifYes1").style.display = "block";
document.getElementById("ifYes2").style.display = "block";
document.getElementById("ifYes3").style.display = "block";
document.getElementById("ifYes4").style.display = "block";
document.getElementById("ifYes5").style.display = "block";
document.getElementById("formstart").scrollIntoView({
behavior: 'smooth'
});
} else {
document.getElementById("ifYes").style.display = "none";
document.getElementById("ifYes1").style.display = "none";
document.getElementById("ifYes2").style.display = "none";
document.getElementById("ifYes3").style.display = "none";
document.getElementById("ifYes4").style.display = "none";
document.getElementById("ifYes5").style.display = "none";
}
// User-Report
if (document.getElementById("yesCheck1").selected) {
document.getElementById("ifYes").style.display = "none";
document.getElementById("ifYes1").style.display = "none";
document.getElementById("ifYes2").style.display = "none";
document.getElementById("ifYes3").style.display = "none";
document.getElementById("ifYes4").style.display = "none";
document.getElementById("ifYes5").style.display = "none";
document.getElementById("ifYes6").style.display = "block";
document.getElementById("ifYes7").style.display = "block";
document.getElementById("ifYes8").style.display = "block";
document.getElementById("ifYes9").style.display = "block";
document.getElementById("ifYes10").style.display = "block";
document.getElementById("ifYes11").style.display = "block";
document.getElementById("formstart").scrollIntoView({
behavior: 'smooth'
});
} else {
document.getElementById("ifYes6").style.display = "none";
document.getElementById("ifYes7").style.display = "none";
document.getElementById("ifYes8").style.display = "none";
document.getElementById("ifYes9").style.display = "none";
document.getElementById("ifYes10").style.display = "none";
document.getElementById("ifYes11").style.display = "none";
}
// No Check
if (document.getElementById("noCheck").selected) {
document.getElementById("ifYes").style.display = "none";
document.getElementById("ifYes1").style.display = "none";
document.getElementById("ifYes2").style.display = "none";
document.getElementById("ifYes3").style.display = "none";
document.getElementById("ifYes4").style.display = "none";
document.getElementById("ifYes5").style.display = "none";
} else {
document.getElementById("ifYes").style.display = "block";
document.getElementById("ifYes1").style.display = "block";
document.getElementById("ifYes2").style.display = "block";
document.getElementById("ifYes3").style.display = "block";
document.getElementById("ifYes4").style.display = "block";
document.getElementById("ifYes5").style.display = "block";
}
}
<div class="form-row">
<div class="form-group col">
<label class="mb-1 text-6"><img class="rounded" src="https://image.flaticon.com/icons/png/128/2222/2222355.png" height="30" width="30"> - Wie können wir dir helfen?</label>
<div class="custom-select-1">
<!-- Here is the JavaScript function started -->
<select onchange="yesnoCheck(this);" class="form-control h-auto" name="city" required>
<option id="noCheck" value="">Thema auswählen..</option>
<option id="yesCheck" value="Allgemeine-Frage">📚 - Allgemeine Frage zu Bl4cklist</option>
<option id="yesCheck1" value="User-Report">🚨 - Server-Mitglied verstößt gegen Regeln</option>
<option id="yesCheck2" value="Entsperrungs-Antrag">🔐 - Mein Account wurde vom Server gesperrt</option>
</select>
</div>
</div>
</div>
<!-- Allgemeine-Frage -->
<div class="form-row">
<div class="form-group col-lg-6" id="ifYes" style="display: none;text-align: left;">
<label class="mb-1 text-4">Mein Name</label>
<div class="position-relative">
<i class="far fa-user-circle text-color-primary text-3 position-absolute left-10 top-50pct transform3dy-n50"></i>
<input type="text" value="" placeholder="Max Mustermann" maxlength="100" class="form-control text-3 h-auto py-2" name="name" required style="padding:35px;">
<div class="help-tip" style="top:-30px;left:97px;">
<p>Trage ihn ein, damit wir dich persönlich ansprechen können.</p>
</div>
</div>
</div>
<div class="form-group col-lg-6" id="ifYes1" style="display: none;text-align: left;">
<label class="mb-1 text-4">Meine E-Mail Adresse</label>
<div class="position-relative">
<i class="far fa-paper-plane text-color-primary text-3 position-absolute left-10 top-50pct transform3dy-n50"></i>
<input type="email" value="" placeholder="[email protected]" maxlength="100" class="form-control text-3 h-auto py-2" name="email" required style="padding:35px;">
<div class="help-tip" style="top:-30px;left:175px;">
<p>Trage hier deine E-Mail ein, damit wir dir antworten können.</p>
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-lg-6" id="ifYes2" style="display: none;text-align: left;">
<label class="mb-1 text-4">Überschrift</label>
<div class="position-relative">
<i class="far fa-edit text-color-primary text-3 position-absolute left-10 top-50pct transform3dy-n50"></i>
<input type="text" value="" placeholder="Frage zu eurem Support-Bot" maxlength="50" class="form-control text-3 h-auto py-2" name="subject" required style="padding:35px;">
<div class="help-tip" style="top:-30px;left:93px;">
<p>Erkläre hier kurzgefasst worum es bei deinem Anliegen geht.</p>
</div>
</div>
</div>
<div class="form-group col-lg-6" id="ifYes3" style="display: none;text-align: left;">
<label class="mb-1 text-4">Thema deiner Frage</label>
<div class="custom-select-1">
<select class="form-control text-3 h-auto" name="city" maxlength="100" required>
<option value="">Thema auswählen..</option>
<option value="Webseite">Unsere Webseite</option>
<option value="Discord-Bots">Unsere Discord-Bots</option>
<option value="Discord-Server">Unser Discord-Server</option>
</select>
</div>
</div>
</div>
<div class="form-row" id="ifYes4" style="display: none;">
<div class="form-group col">
<label class="mb-1 text-4">Erkläre nun dein Anliegen..</label>
<div class="position-relative">
<i class="far fa-comment-dots text-color-primary text-3 position-absolute left-10 top-50pct transform3dy-n50"></i>
<textarea maxlength="2000" rows="5" placeholder="Verfasse eine Nachricht.." class="form-control text-3 h-auto py-2" name="message" required style="padding:35px;"></textarea>
</div>
</div>
</div>
<div class="form-row" id="ifYes5" style="display: none;">
<div class="form-group col">
<br />
<input type="submit" name="finish1" value="Meine Frage abschicken" class="btn btn-primary btn-modern" data-loading-text="Laden..">
</div>
</div>
<!-- User-Report -->
<div class="form-row">
<div class="form-group col" id="ifYes6" style="display: none;text-align: left;">
<label class="mb-1 text-4">Mein Discord-Name
<div class="help-tip" style="left:105px;"><p>Trage ihn ein, damit wir dich persönlich ansprechen können.</p></div>
</label>
<input type="text" value="" placeholder="Max Mustermann" maxlength="100" class="form-control text-3 h-auto py-2" name="name" required>
</div>
</div>
<div class="form-row">
<div class="form-group col" id="ifYes7" style="display: none;text-align: left;">
<label class="mb-1 text-4">Meine E-Mail Adresse
<div class="help-tip" style="left:180px;"><p>Trage hier deine E-Mail ein, damit wir dir antworten können.</p></div>
</label>
<input type="email" value="" placeholder="[email protected]" maxlength="100" class="form-control text-3 h-auto py-2" name="email" required>
</div>
</div>
<div class="form-row">
<div class="form-group col" id="ifYes8" style="display: none;text-align: left;">
<label class="mb-1 text-4">Überschrift
<div class="help-tip" style="left:105px;"><p>Erkläre hier kurzgefasst worum es bei deinem Anliegen geht.</p></div>
</label>
<input type="text" value="" placeholder="Frage zu eurem Support-Bot" maxlength="50" class="form-control text-3 h-auto py-2" name="subject" required>
</div>
</div>
<div class="form-row">
<div class="form-group col" id="ifYes9" style="display: none;text-align: left;">
<label class="mb-1 text-4">Thema deiner Frage</label>
<div class="custom-select-1">
<select class="form-control h-auto" name="city" maxlength="100" required>
<option value="">Thema auswählen..</option>
<option value="Webseite">Unsere Webseite</option>
<option value="Discord-Bots">Unsere Discord-Bots</option>
<option value="Discord-Server">Unser Discord-Server</option>
</select>
</div>
</div>
</div>
<div class="form-row" id="ifYes10" style="display: none;">
<div class="form-group col">
<label class="mb-1 text-4">Erkläre nun dein Anliegen..</label>
<textarea maxlength="2000" rows="5" class="form-control text-3 h-auto py-2" name="message" required></textarea>
</div>
</div>
<div class="form-row" id="ifYes11" style="display: none;">
<div class="form-group col">
<br />
<input type="submit" name="finish1" value="Meine Frage abschicken" class="btn btn-primary btn-modern" data-loading-text="Laden..">
</div>
</div>
forloopsif/thenlogic. You don't need 3if/then, it should beif/else if/else if/else