What if I want to target all images? I want to show Alt text on mouse hover on all images, and if there is no Alt text just CompanyName, is it possible?
Got this script, but not sure what to correct:
<script>
document.addEventListener('DOMContentLoaded', function() {
const images = document.querySelectorAll('img');
images.forEach(img => {
const altText = img.getAttribute('alt') || 'Decoflame';
img.setAttribute('title', altText);
// Optional: Custom tooltip for hover
img.addEventListener('mouseover', function() {
const tooltip = document.createElement('div');
tooltip.textContent = altText;
tooltip.style.position = 'absolute';
tooltip.style.background = 'white';
tooltip.style.border = '1px solid black';
tooltip.style.padding = '5px';
document.body.appendChild(tooltip);
img.addEventListener('mousemove', function(e) {
tooltip.style.top = e.pageY + 10 + 'px';
tooltip.style.left = e.pageX + 10 + 'px';
});
img.addEventListener('mouseout', function() {
document.body.removeChild(tooltip);
});
});
});
});
</script>