Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make newly cloned buttons work with an existing eventlistener?

On the press of the button ‘leave’ I want 10 new ‘leave’ buttons to be displayed on the screen. Each one of these buttons needs to display 10 new buttons on the screen when clicked.

But only the original ‘leave’ button displays 10 new buttons, the newly cloned ones don’t.

Javascript code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');

leaveButton.addEventListener('click', () => {
    for (let i = 0; i < 10; i++) {
        const newButton = document.createElement('button');
        newButton.innerHTML = leaveButton.innerHTML;
        newButton.style.top = Math.random() * window.innerHeight + 'px';
        newButton.style.left = Math.random() * window.innerWidth + 'px';
        newButton.setAttribute("id", "leave");
        container.appendChild(newButton);
    }
});

HTML code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PressTheButton</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <button id="leave">Leave</button>
        <h1>It's simple</h1>
        <p>Just click the buttons</p>
    </div>
    <script src="script.js"></script>
</body>
</html>

>Solution :

Instead of adding the event listener to a fixed button you should work with delegated event listening. In the snippet below I added the click event to the parent container. Inside the event listener callback function I briefly check, wether the .textContent of the clicked element is equal to "Leave" and, if not, I return immediately without carrying out any action.

const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');

container.addEventListener('click', ev => {
if (ev.target.textContent!=="Leave") return;
for (let i = 0; i < 10; i++) {
    const newButton = document.createElement('button');
    newButton.innerHTML = leaveButton.innerHTML;
    newButton.style.top = Math.random() * window.innerHeight + 'px';
    newButton.style.left = Math.random() * window.innerWidth + 'px';
    newButton.setAttribute("id", "leave");
    container.appendChild(newButton);
}
});
button {position:absolute}
<head>
<meta charset="UTF-8">
<title>PressTheButton</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
    <button id="leave">Leave</button>
    <h1>It's simple</h1>
    <p>Just click the buttons</p>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading