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

123
Visualizações
Get input elements and add click-event

I have this (bootstrap) form with three elements. As the button is clicked, another line with three elements is added.

<div class="row align-items-center mb-2 mt-2 ms-1 ">                                        
    <div class="col-5 ps-1 pe-1">
        <select class="form-select example-select" id="cmbSelect[]" name="cmbSelect[]">
           <option>1</option>
           <option>2</option>
           <option>3</option>
        </select>
    </div>
    <div class="col-4 ps-1 pe-1">
        <input class="form-control" type="text" name="txtFirst[]">
    </div>
    <div class="col-2 ps-1 pe-1">
        <input class="form-control" type="text" name="txtSecond[]" value="3">
    </div>
    <div class="col-1 ps-1 pe-1">                       
        <a href="javascript:void(0);" class="add_button" title="Add one more element"><img src="../img/add-icon.png"/ alt="Add"></a>
    </div>
</div>

I would like to execute a JS function if the first element cmbSelect[] is clicked. The function must get the element that triggered the event, because its selected item should be edited. Furthermore the value of the element next to the one that triggered the event should be updated.

What is the best way doing this?

I tried something like this:

        var example_select = $(".example-select");

        var all = $(".example-select").map(function() {
            this.on("click", function (element) {
                console.log("clicked");
                return element.innerHTML;
            });                
        }).get();            
        console.log(all.join());

But example_select is empty and the click event is not fired...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

map is not used like this, you will get Uncaught TypeError: this.on is not a function

You just need to use on to listen for event

e.g.

var example_select = $(".example-select");

$('input[type=text]').on('input', function(element) {
  console.log("input");
});

$('select').on('change', function(element) {
  const isFirst = $(this).children('option:first-child').is(':selected');
  console.log(`The current option val: ${$(this).val()}`);
  console.log(`Is it the first option: ${isFirst}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row align-items-center mb-2 mt-2 ms-1 ">
  <div class="col-5 ps-1 pe-1">
    <select class="form-select example-select" id="cmbSelect[]" name="cmbSelect[]">
      <option>1</option>
      <option>2</option>
      <option>3</option>
    </select>
  </div>
  <div class="col-4 ps-1 pe-1">
    <input class="form-control" type="text" name="txtFirst[]">
  </div>
  <div class="col-2 ps-1 pe-1">
    <input class="form-control" type="text" name="txtSecond[]" value="3">
  </div>
</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