Hey Tejaswi,
You need to ensure the DOM elements are available when the script runs. The setTimeout you used might not always be reliable. Also make sure the paginationBar and its children are correctly selected. Finally, make sure that event listeners are correctly attached to the elements.
Try to use this code:
document.addEventListener("DOMContentLoaded", function() {
function ImageControl() {
console.warn("DOM Content Loaded");
let paginationBar = document.querySelector('.pagination-bar');
if (!paginationBar) {
console.warn("Pagination bar not found.");
return;
}
console.warn("Pagination bar found.");
let children = paginationBar.children;
for (let i = 0; i < children.length; i++) {
let currentIndex = i;
children[i].addEventListener('click', function(event) {
if (currentIndex === 0) {
setTimeout(() => {
children[3].focus();
}, 600);
} else if (currentIndex === 4) {
setTimeout(() => {
children[0].focus();
}, 600);
} else {
children[currentIndex].focus();
}
});
}
}
// Ensure ImageControl is called after DOM is fully loaded
setTimeout(() => {
ImageControl();
}, 1000); // Adjust timeout if necessary
});
This code may not complety solve your issues but it should help :)
If this answer helped, mark as "accepted"
Best Regards,
Ricardo Pereira