Using a click listener when a button has have a settable ID
Your button ID
MyButtonId
Your handler
<script>
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('MyButtonId').addEventListener('click', function()
{
//Do something...
});
});
</script>
Using a click listener when a button doesn’t have a settable ID
Your buttons link (its href=)
#MyButtonLink
Your handler
<script>
document.addEventListener("DOMContentLoaded", function()
{
document.addEventListener('click', function(Event)
{
const Button = Event.target.closest('a[href="#MyButtonLink"]');
if (!Button)
return;
Event.preventDefault();
//Do something...
});
});
</script>
