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>