Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Show and hide input fields depending on which Radio Button you select

i can't get it to work. I want the User to choose between two options (radio buttons). The first option should only show one input field with the label Text of "Höhe". The second Option should change the label Text of the first input to "Niedrigste Höhe in mm" and show the second input field.

Maybe someone can spare a few minutes and help me, thanks.

Here is my Code, but it won't work..

function adjustableHeightCheck() {
  if (document.getElementById("adjustableHeight").checked) {
    document.getElementById("max-height").style.display = "block";
    document.getElementById("height").innerHTML = "Niedrigste Höhe in mm";
  } else {
    document.getElementById("max-height").style.display = "none";
  }
}
<label>Fix Höhe
  <input type="radio" name="verlegeart">
</label>

<label>Verstellbare Höhe
  <input type="radio" name="verlegeart" id="adjustableHeight">
</label>

<br>

<label>
  <p id="height">Höhe</p>
  <input type="number">
</label>

<div id="max-height" style="display: none">
  <label>
    <p>Höchste Höhe in mm</p>
    <input type="number">
  </label>
</div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just add the onclick event handler to get thing done:

<label>Fix Höhe
  <input type="radio" name="verlegeart" onclick="adjustableHeightCheck()">
</label>

<label>Verstellbare Höhe
  <input type="radio" name="verlegeart" id="adjustableHeight"  onclick="adjustableHeightCheck()">
</label>
about 4 years ago · Juan Pablo Isaza Relatório

0

You need an event listener for that because the function isn't called just by declaration. You could do that inline with:

<input type="radio" name="verlegeart" id="adjustableHeight"  onclick="adjustableHeightCheck()">

But this isn't good practice – HTML, CSS and JavaScript should be separate:

const adjustableHeight = document.querySelector('#adjustableHeight');

adjustableHeight.addEventListener('change', adjustableHeightCheck);

You also forgot to change the label back in the else statement:

document.getElementById("height").innerHTML = "Höhe";

Working example: (CSS also separate ;)

const fixHeight = document.querySelector('#fixHeight');
const adjustableHeight = document.querySelector('#adjustableHeight');

fixHeight.addEventListener('change', adjustableHeightCheck);
adjustableHeight.addEventListener('change', adjustableHeightCheck);

function adjustableHeightCheck() {
  if (document.getElementById("adjustableHeight").checked) {
    document.getElementById("max-height").style.display = "block";
    document.getElementById("height").innerHTML = "Niedrigste Höhe in mm";
  } else {
    document.getElementById("max-height").style.display = "none";
    document.getElementById("height").innerHTML = "Höhe";
  }
}
#max-height {
  display: none;
}
<label>Fix Höhe
  <input type="radio" name="verlegeart" id="fixHeight">
</label>

<label>Verstellbare Höhe
  <input type="radio" name="verlegeart" id="adjustableHeight">
</label>

<br>

<label>
  <p id="height">Höhe</p>
  <input type="number">
</label>

<div id="max-height">
  <label>
    <p>Höchste Höhe in mm</p>
    <input type="number">
  </label>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda