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

263
Views
How to add more inputs depend dropdown values (PHP, JS)

so I want to add some input based on the option value from the drop down that will be selected. For example, the dropdown looks like this:

<form action="#" method="post">
    <select name="job" id="job" >
        <option value="">position</option>
        <option value="1">Teacher</option>
        <option value="2">Principal</option>
        <option value="3">Staff</option>      
    </select>
</form>

and if the selected value is number 1 or teacher, it will display new input like this below it:

<input type="number" name="student" id="student">
<input type="Text" name="class" id="class">

if you guys know how to make it, maybe you can help me with this please, either using php or js because I've been stuck with this problem for a long time.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

There are many different ways to achieve what you want depends on what framwwork you are using.

For

  • vanilla JS: onchange to control css OR append html.
  • jQuery: $("#class").hide() / .show();
  • Angular: ngIf
  • vueJs: v-if / v-show

etc...

about 4 years ago · Juan Pablo Isaza Report

0

before that, set the style of input is hide, then You can try onchange on select, Which is get the value of select. then proceed to show the hide input.

const job = document.getElementById("job");
let add = document.getElementsByClassName("addEx");

job.addEventListener("change",function() {
    if (this.value == 1) {
       for (let i = 0; i < add.length; i++) {
           add[i].style.display = 'block';
       } 
    } 
    else {
        for (let i = 0; i < add.length; i++) {
            add[i].style.display = 'none';
        } 
    }
});
.addEx {
    display:none;
}
<form action="#" method="post">
    <select name="job" id="job" >
        <option value="">position</option>
        <option value="1">Teacher</option>
        <option value="2">Principal</option>
        <option value="3">Staff</option>      
    </select>
    <input type="number" name="student" id="student" class="addEx">
    <input type="Text" name="class" id="class" class="addEx">
</form>

about 4 years ago · Juan Pablo Isaza Report

0

You can use like this:

jQuery(document).ready(function() {
  $('#job').change(function() {
    let job = $('#job').val();
    if (job == '1') {
      $('#sampleBox').show();
    } else {
      $('#sampleBox').hide();
    }
  });
});
#sampleBox {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="job" id="job">
  <option selected hidden disabled>Position</option>
  <option value="1">Teacher</option>
  <option value="2">Principal</option>
  <option value="3">Staff</option>
</select>

<div id="sampleBox">
  <input type="number" name="student" id="student">
  <input type="Text" name="class" id="class">
</div>

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!