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

104
Visualizações
Do I need to refactor this javascript for conditional form logic?

I have a form in Laravel that goes over several template (blades). For each template I am implementing simple conditional logic to show and hide groups of questions.

For the templates, I'm using HTML5 for efficient client-side validation. I wrote the javascript below to show and hide fields using HTML attributes.

The example below responds to a yes/no question by exposing hidden sets of radio buttons. One set is for answering Yes, one is for No.

My question is, do I need to refactor this? Is there a more efficient way I should be writing this javascript, or am I doing ok?

I'm using an event listener so that clicks can bubble up, understanding that this is most efficient. Since I have more than one page, I have more than one event listener, one for each template. Is there a better way to do that?

Thanks for your input.

    // "Purpose of Notification" Blade
    document.addEventListener('click', function (event) {
      // When yes or no is choosen
      if (event.target.matches('#waiver30100byes')) {
        // show the patient level group container
        document.getElementById("patient-level").removeAttribute("hidden");
        // and show 100 level for yes and make it required
        document.getElementById("patient-level-100").removeAttribute("hidden");
        document.getElementById("level1").required = true;
        // hide and un-require the 30 level radio group
        document.getElementById("patient-level-30").hidden = true;
        document.getElementById("level3").required = false;
      }
      else if (event.target.matches('#waiver30100bno')) {
        // show the patient level group
        document.getElementById("patient-level").removeAttribute("hidden");
        // and show 30 level for no and make it required
        document.getElementById("patient-level-30").removeAttribute("hidden");
        document.getElementById("level3").required = true;
        //hide and un-required the 100 level radio group
        document.getElementById("patient-level-100").hidden = true;
        document.getElementById("level1").required = false;
        //
      }
    }, false);
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