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

274
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
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