How to reference dynamically created elements with jQuery

<body>
<div id="my-div">
</div>
</body>
$('#my-div').append('<button id="my-button">My Button</button>');
$('#my-div').on('click', '#my-button', function () {

alert('Hello World!');
});

--

--

Creator of Goalsmash.net. Help others achieve their goals. Get paid.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joel Masters

Joel Masters

Creator of Goalsmash.net. Help others achieve their goals. Get paid.