Home > Mobile >  How to get id of a dynamic input field in javascript
How to get id of a dynamic input field in javascript

Time:03-22

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 >
  <div >
    <label>Category</label>
    <input type="text" name="category" id="category" placeholder="Classes/Instances" required/>
    <ul ></ul>
  </div>
  <div >
    <label>Relation</label>
    <input type="text" name="relation" id="relation" placeholder="Properties" required/>
    <ul ></ul>
  </div>
  <div >
    <label>Value</label>
    <input type="text" name="value" id="value" placeholder="Classes/Instances" required/>
    <ul ></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

CodePudding user response:

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);
});

CodePudding user response:

You can use the onfocus event

<div >
  <div >
    <label>Category</label>
    <input type="text" name="category" id="category" placeholder="Classes/Instances" onfocus="onFocusCategoryInput()" required/>
    <ul ></ul>
  </div>
  <div >
    <label>Relation</label>
    <input type="text" name="relation" id="relation" placeholder="Properties" onfocus="onFocusRelationInput()" required/>
    <ul ></ul>
  </div>
  <div >
    <label>Value</label>
    <input type="text" name="value" id="value" placeholder="Classes/Instances" onfocus="onFocusValueInput()" required/>
    <ul ></ul>
  </div>
</div>

  • Related