I don't know what it is about me, but I'm flawed in this way, and I'm sure I'm
not alone. Big projects are fun but they can also be daunting.
-{. .image-med}
+{. .image-med}
## Palate Cleansers FTW
function main() {
- addCaptions();
+ var imageNodes = document.querySelectorAll("img.image-med");
+ var images = [];
+
+ imageNodes.forEach((image) => {
+ images.push(new InteractiveImage(image));
+ });
}
-// Create a class that, when given an element, adds caption based
-// upon the alt text of the image, click events, etc.
+class InteractiveImage {
+
+ constructor(imageElement) {
+ this.image = imageElement;
-function addCaptions() {
- var images = document.querySelectorAll("img.image-med");
+ this.addCaption();
+ this.addEvents();
+ }
- images.forEach((image) => {
- var parent = image.parentElement;
- var target = document.createElement("div");
+ addCaption() {
+ var container = document.createElement("figure");
+ var caption = document.createElement("figcaption");
- parent.setAttribute("class", "image-content");
- target.setAttribute("class", "caption");
+ caption.appendChild(
+ document.createTextNode(this.image.attributes.alt.value));
- target.appendChild(
- document.createTextNode(image.attributes.alt.value));
- parent.appendChild(target);
+ var parent = this.image.parentElement;
+ container.appendChild(this.image);
+ container.appendChild(caption);
- image.addEventListener("click", (event) => {
+ parent.replaceWith(container);
+ }
+
+ addEvents() {
+ this.image.addEventListener("click", (event) => {
console.log(event.target);
});
- });
+ }
}
addEventListener("DOMContentLoaded", main);