Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda