1

I set button which id will be switched by clicking them.

To achieve this, I worked like below code. In my work, clicked id will be generated dynamically.

I would like to know how to select generated element in javascript?

Thanks

document.getElementById("button").addEventListener('click',function(){
  this.id="clicked"
});

document.getElementById("clicked").addEventListener('click',function(){
  this.id="button"
});
<button id='button'>button</button>
Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • 1
    Dynamically switching the ID of an existing element is a *very* strange thing to want to do. What are you trying to accomplish here with the clicks? – CertainPerformance May 09 '20 at 08:42
  • Changing the `id` is a very bad idea to begin with, but it won't change the reference which you already bound a listener to. You second attempt at adding an event listener will fail with an error message stating the likes of `addEventListener of undefined is not a function`. Also, note that you are not generating any elements in your code. – connexo May 09 '20 at 08:47

0 Answers0