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

166
Visualizações
Can't change radiobuttons display to none again

i got a container with 3 radionbuttons and 2 of the radiobuttons have another radiobuttons next to them but their displays are none . When i click the radiobutton i wanna display the hidden radiobuttons and when i click another radionbutton i want it's display to be none again .

`function showHiddenText() 
{
  if (document.getElementById("diger").checked) {
    const hiddenInput = document.getElementById("hidden-input");
    hiddenInput.style.display = "block";
  } else if (document.getElementById("diger").checked == false) {
    hiddenInput.style.display = "none";
  }
}`

It's a simple function i write to do that but else if part is not working . When i display hidden radio buttons they dont disappear ever again. radionbuttons hiddenpart

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

0

const variables are block scoped. So, in the else if statement hiddenInput is undefined.

Move it above the statements

function showHiddenText() 
{
  const hiddenInput = document.getElementById("hidden-input");
  if (document.getElementById("diger").checked) {
    hiddenInput.style.display = "block";
  } else if (document.getElementById("diger").checked == false) {
    hiddenInput.style.display = "none";
  }
}

By the way, you can make it simpler by replacing the else if statement with else:

function showHiddenText() 
{
  const hiddenInput = document.getElementById("hidden-input");
  if (document.getElementById("diger").checked) {
    hiddenInput.style.display = "block";
  } else {
    hiddenInput.style.display = "none";
  }
}

Or even more:

function showHiddenText() 
{
  const hiddenInput = document.getElementById("hidden-input");
  if (document.getElementById("diger").checked) {
    hiddenInput.style.display = "block";
    return true; // you can return anything here
  }
  hiddenInput.style.display = "none";
  return false; // and here (it is not necessary to return here)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Make sure your radio buttons have the same name. Radio buttons with same name are compared to each other. Add onclick="showHiddenText()" to inputs, then:

html:

<input type="radio" name="btn" id="diger" onclick="showHiddenText()">
<input type="radio" name="btn" onclick="showHiddenText()">
<input type="radio" id="hidden-input">

js:

function showHiddenText() {
  if (document.getElementById("diger").checked) {
    document.getElementById("hidden-input").style.display = 'block'
  }
  else{
    document.getElementById("hidden-input").style.display = 'none'
  }
}
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