The JavaScript element in Mition allows you to add custom scripts to enhance the functionality and interactivity of your website.
This element is ideal for embedding dynamic features that go beyond standard HTML and CSS.
You must include something in the javascript code at a minimum, even just:
console.log('go!');
function waitForElementAndAddClickListener(elementId, maxRetries = 10) {
let retries = 0;
function checkForElement() {
const targetElement = document.getElementById(elementId);
if (targetElement) {
// Element found! Add a click event listener.
targetElement.addEventListener('click', () => {
alert('Clicked!');
});
} else {
// Element not found.
retries++;
console.log(`Element with ID "${elementId}" not found. Retries: ${retries}`);
if (retries >= maxRetries) {
console.log(`Aborting after ${maxRetries} retries.`);
return;
}
// Retry after 1 second.
setTimeout(checkForElement, 1000);
}
}
// Start checking for the element.
checkForElement();
}
// Usage example:
waitForElementAndAddClickListener('ExampleButton');