0

I have a button that creates two input fields on click. I want to add a unique id for each couple of inputs that is created so that I can delete them later. Currently when I add the inputs they all have the same id 0 and the index does not increment, why and how can I make it increment? Here is my code:

 createNewPricedRoundShareholder() {

  
    const mainParent = document.getElementById('main-parent');
    var index = 0;

    const newPlatformNameInput1 = document.createElement("input");
  newPlatformNameInput1.id = index + '_first';
  newPlatformNameInput1.value = index;
  const newPlatformNameInput2 = document.createElement("input");
  newPlatformNameInput2.id = index + '_second';
  newPlatformNameInput2.value = index;
  const deleteButton = document.createElement("button");
  deleteButton.innerText = 'delete';
  const wrapperParent = document.createElement('div');
  wrapperParent.id = index + '_parent';
  wrapperParent.appendChild(newPlatformNameInput1);
  wrapperParent.appendChild(newPlatformNameInput2);
  wrapperParent.appendChild(deleteButton);  mainParent.appendChild(wrapperParent);
  index++;
    

    }

and my html:

<div id="main-parent"></div>
6
  • simply use index++ to increment Commented Nov 30, 2020 at 15:51
  • I have it in my code and it still doesn't increment correctly Commented Nov 30, 2020 at 15:52
  • But here you are not showing that. Show the original code. Commented Nov 30, 2020 at 15:54
  • 2
    Check this link : stackoverflow.com/questions/42159685/… and stackoverflow.com/questions/19994281/… it may solve your problem Commented Nov 30, 2020 at 15:56
  • 3
    Your index is local to the function so it always resets to 0 when you call it. At the end you increment it to 1 but that has no effect. You have to declare var index = 0 outside the function. Commented Nov 30, 2020 at 15:58

2 Answers 2

0

I know you said you want an ID so you can use it to delete your row later, but you don't actually need it. If you add this code to your function, you can delete the entire row without the need of using an ID. This code will allow you to target the specific button, then the parent of that button and remove it.

deleteButton.addEventListener("click",function(e){
  e.preventDefault();
  e.target.parentNode.remove();
});
Sign up to request clarification or add additional context in comments.

Comments

0

I think that is because you increase the index at the end of your function but you should increase it after creating the first input and before creating the second one

    [...]
    const newPlatformNameInput1 = document.createElement("input");
    newPlatformNameInput1.id = index + '_first';
    newPlatformNameInput1.value = index;
    index++;
    const newPlatformNameInput2 = document.createElement("input");
    newPlatformNameInput2.id = index + '_second';
    [...]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.