Thread Starter
tonyuk
(@tonycsanders)
Not strictly an MLA question I guess. Scripted in an event listener. Any more elegant solutions gratefully accepted.
<script>
var links = document.getElementsByClassName("gallery-caption");
function changeColor(e) {
e.target.style.color = e.target.style.color = 'red';
}
for (var i = 0; i < links.length; i++) {
links[i].addEventListener('click', changeColor);
}
</script>
<script>
var thumbnails = document.getElementsByClassName("gallery-icon");
function changeColorIcon(e) {
e.target.style.backgroundColor = e.target.style.backgroundColor = 'red';
}
for (var i = 0; i < thumbnails.length; i++) {
thumbnails[i].addEventListener('click', changeColorIcon);
}
</script>
-
This reply was modified 3 months, 2 weeks ago by
tonyuk.
Thread Starter
tonyuk
(@tonycsanders)
This is better. Gallery icon and caption text get a blue background when either the thumbnail or caption is clicked. My scripting skills didn’t have the mileage for this – chatgpt however did.
<script>
document.addEventListener("DOMContentLoaded", function () {
// Select all image links and captions
const galleryItem = document.getElementsByClassName("gallery-item");
const captions = document.getElementsByClassName("gallery-caption");
// Convert HTMLCollection to an array and loop through
for (let i = 0; i < galleryItem.length; i++) {
// Ensure there is a corresponding caption for each image
if (captions[i]) {
// Click event on image link
galleryItem[i].addEventListener("click", function (event) {
this.style.backgroundColor = "lightblue"; // Change image background
});
// Click event on caption
captions[i].addEventListener("click", function (event) {
galleryItem[i].style.backgroundColor = "lightblue"; // Change image background
});
}
}
});
</script>
Good to hear from you again. Thanks for working through this issue on your own before I had a chance to respond. I was going to suggest some sort of CSS and script solution, and you’ve found it!
I’m impressed that ChatGPT was able to help – it’s a new world. I will leave this topic resolved, but please update it if you have problems, further questions or more solutions to share. Thanks for your continued interest in the plugin.