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

153
Views
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 answers
Answer question

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 Report

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 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!