Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!