Click Event That Runs Even On New Elements
Utility
JS
- Normally JavaScript only runs for the elements that were initially there on page load. So any new elements that are added to the page through filtering, looped sliders, or fetched elements wouldn't have the interaction applied. By applying the click event to the document instead and filtering it to a certain class, the action will also apply to any new elements added to the page with that class.
<script>
document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("click", (e) => {
if (!e.target.closest(".your-element")) return;
e.target.classList.toggle("is-active");
});
});
</script>Preview Links
Custom JS
Enable custom code in preview or view on published site.
Enable custom code?
Last Updated: Dec 4, 2025
- Normally JavaScript only runs for the elements that were initially there on page load. So any new elements that are added to the page through filtering, looped sliders, or fetched elements wouldn't have the interaction applied. By applying the click event to the document instead and filtering it to a certain class, the action will also apply to any new elements added to the page with that class.
<script>
document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("click", (e) => {
if (!e.target.closest(".your-element")) return;
e.target.classList.toggle("is-active");
});
});
</script>