0

I have problem triggering dynamically created button. Event listener only works fine if element is created manually.

<!DOCTYPE html>
<html>
<body>
<button id="create">Create new button</button>
<div id="mydiv"></div>

<script>
document.getElementById("create").addEventListener('click', (x) => {
document.getElementById("mydiv").innerHTML = "<button id='show'>Show alert</button>";});

document.getElementById('show').addEventListener('click', (b) => {
alert("It works");});
</script>
</body>
</html>

//Sorted out with event delegation:

<!DOCTYPE html>
<html>
<body>
<button id="create">Create new button</button>
<div id="mydiv"></div>

<script>
document.getElementById("create").addEventListener('click', (x) => {
    document.getElementById("mydiv").innerHTML = "<button class='show'>Show alert</button>";
});
const parent = document.getElementById('mydiv');
parent.addEventListener('click', (b) => {
if (x.target.className === 'show') {alert("It works")}
;});
</script>
</body>
</html>
3
  • 1
    You might want to try using event delegation instead? Commented Dec 30, 2019 at 19:00
  • I want to create one page website were would be many different containers which will refresh or disappears or appears triggered by event listener. Commented Dec 30, 2019 at 19:17
  • Thanks event delgation works fine. Commented Dec 30, 2019 at 19:38

2 Answers 2

1

You must move the code that binds the new element into the event handler for the code that creates it because when the event binding for the new element is outside, it fails immediately because the element doesn't exist.

In other words, you have to wait for the element to exist before you can bind it.

<!DOCTYPE html>
<html>
<body>
<button id="create">Create new button</button>
<div id="mydiv"></div>

<script>
document.getElementById("create").addEventListener('click', (x) => {

  document.getElementById("mydiv").innerHTML = "<button id='show'>Show alert</button>";
  
  // This code won't be run until after the new element is created, so it will
  // be safe to reference it at that point.
  document.getElementById('show').addEventListener('click', (b) => {
    alert("It works");
  });
  
});


</script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<!DOCTYPE html>
<html>
<body>
<button id="create">Create new button</button>
<div id="mydiv"></div>
<script>
document.getElementById("create").addEventListener('click', (x) => {
document.getElementById("mydiv").innerHTML = "<button id='show'>Show alert</button>";});
document.addEventListener('click',function(e){
    if(e.target && e.target.id== 'show'){
        alert("It works");
     }
 });
</script>
</body>
</html>

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.