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

154
Visualizações
How to get id of a dynamic input field in javascript

I want to create a dynamic form where I also apply JavaScript to the dynamic elements.

What I want to do right now is figure out which field (stored in array or whatever data structure) in JavaScript has been clicked so that I can apply the script to that particular field.

The HTML looks like this:

<div class="triple">
  <div class="sub-triple">
    <label>Category</label>
    <input type="text" name="category" id="category" placeholder="Classes/Instances" required/>
    <ul class="cat-list"></ul>
  </div>
  <div class="sub-triple">
    <label>Relation</label>
    <input type="text" name="relation" id="relation" placeholder="Properties" required/>
    <ul class="rel-list"></ul>
  </div>
  <div class="sub-triple">
    <label>Value</label>
    <input type="text" name="value" id="value" placeholder="Classes/Instances" required/>
    <ul class="val-list"></ul>
  </div>
</div>

This "triple" div is dynamic and I want to create as many "triples" as the user wants, that also means the input fields of inside the "triple" section increase as well.

I'm confused on how to add javascript to one element of the input. For example I have inputs: category, relation and value and the user wanted 2 or more triples then the input ids could look like category2, relation2 and value2 or something similar to that.

let category = document.getElementById("category");

category.addEventListener("keyup", (e) => {
  removeElements();
  listDown(category, sortedClasses, ".cat-list")

});

If I lets say clicked on category2 for instance how do I tell that to my javascript since these fields are completely dynamic.

Summary: The user is adding repeat sections of triples (containing 3 input elements), where the id of each input element is generated dynamically. My script above works for the first triple section only as the ids are fixed, as for successive "triple" section the id of the fields get changed. How do I identify (see which element has been clicked) and get these dynamic ids

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

0

Try listening to the parent element and then using the event's target to figure out the identity of the input. Since the child elements events will bubble up you'll be able to listen to all children

e.g.

let parentElement = document.querySelector(".triple");
parentElement.addEventListener("click", (e) => {
    console.log(e.target.id);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the onfocus event

<div class="triple">
  <div class="sub-triple">
    <label>Category</label>
    <input type="text" name="category" id="category" placeholder="Classes/Instances" onfocus="onFocusCategoryInput()" required/>
    <ul class="cat-list"></ul>
  </div>
  <div class="sub-triple">
    <label>Relation</label>
    <input type="text" name="relation" id="relation" placeholder="Properties" onfocus="onFocusRelationInput()" required/>
    <ul class="rel-list"></ul>
  </div>
  <div class="sub-triple">
    <label>Value</label>
    <input type="text" name="value" id="value" placeholder="Classes/Instances" onfocus="onFocusValueInput()" required/>
    <ul class="val-list"></ul>
  </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