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
Function with arguments not working when used inside event listener

I have 2 radio buttons (YES) (NO) and an input (PLEASE EXPLAIN) The input is hidden and the intention is to make it visible only if you select (YES) I want to create a function with parameters so that it can be reused. I don't know what's wrong with my code looks correct to me but the input field doesn't appear if you click (YES) and I see no errors

HTML CODE

<div>
  <strong class="input-group-title">Have you had any surgeries?</strong>
</div>
<div>
  <div>
    <input type="radio" id="any_surgeries_yes" value="Yes">
    <label>Yes</label>
  </div>
  <div>
   <input type="radio" id="any_surgeries_no" value="No">
   <label>No</label>
  </div>
</div>

<div class="pop-up-field toggle-content" id="explain-surgeries">
   <input type="text" name="please-explain">
   <label>Please Explain</label>
</div>

CSS CODE

.toggle-content {
    display: none;
}

.toggle-content.is-visible {
    display: block;
}

.pop-up-field {
    /*empty*/
}

JAVASCRIPT CODE

const conditionalToggler = function(condYes,condNo,toggableField) {
    if (condYes.checked) {
      toggableField.classList.add('is-visible');
  
      } else if(condNo.checked){
      toggableField.classList.remove('is-visible');
      }
    };
  

const surgeriesYes = document.getElementById('any_surgeries_yes');
const surgeriesNo = document.getElementById('any_surgeries_no');
const explainSurgeries = document.getElementById('explain-surgeries');


surgeriesYes.addEventListener('change',conditionalToggler(surgeriesYes, surgeriesNo, explainSurgeries));
surgeriesNo.addEventListener('change',conditionalToggler(surgeriesYes, surgeriesNo, explainSurgeries));
about 4 years ago · Juan Pablo Isaza
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