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

179
Views
Show hidden fields on a html questionnaire form

I've created a questionnaire, which allows users to answer YES or NO to each question. if NO is clicked, field is hidden. But if YES is clicked, I want to show hidden field which has further instructions for each question.

The following code works for the first question, but not for subsequent question.

How can I amend the script to work for all questions on the questionnaire.

function showhideInfo()
{
if (document.getElementById('inlineRadio1').checked) {
     document.getElementById('reqdfield').style.display = 'block';
     }
     else document.getElementById('reqdfield').style.display = 'none';
}

Here is my html code

    <div class="input-group">
      <b><label class = "label">Have you attended this course before</label></b>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="YES" onclick="showhideInfo()">
          <label class="form-check-label" for="inlineRadio1">YES</label>
        </div>
        <div class="form-check form-check-inline">
          <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="NO" onclick="showhideInfo()">
          <label class="form-check-label" for="inlineRadio2">NO</label>
        </div>
      <div class="regdInfo" id="reqdfield" style="display:none">
        You will need to provide details of previous course attended.
      </div>
    </div> 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ids are (supposed to be) unique on a web page. In particular getElementById will return a single element only, even if there were multiple elements having the same ID in the document.

Use a selector based on the class instead. Eg:

document.querySelectorAll('.reqdInfo').forEach ( pe => { pe.style.display = 'none'; /* or 'block' */ });
about 4 years ago · Juan Pablo Isaza Report
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!