There's probably something very important I'm missing here, but I've been working on this code and I keep getting stuck with the same errors over and over again--I'm not sure what I'm doing wrong.
Here is my HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Images to Text</title>
<link rel="stylesheet" href="styles.css" type="text/css"/>
</head>
<body>
<h1>Some of my Favorite Animals.</h1>
<h2>Capybara & Mantis Shrimp</h2>
<div id='showAlt'>
<img src="capybaraman.jpg" height="190" alt="A picture of a capybara in the arms of a man, showing that they are about the size of dogs but look comparable to guinea pigs.">
<img src="capybaramonkey.jpg" width="300" alt="A picture of a capybara with monkeys petting and on top of the capybara.">
<img src="capybarabird.png" width="300" alt="A picture of a capybara laying in the mud. There is also a bird sitting on top of them.">
<img src="mantisshrimp.jpg" width="300" alt="A picture of a mantis shrimp with its' arms open, they are rainbow with large eyes. They have small wing-like structures coming from the sides of their bodies.">
<img src="mantisbody.jpg" width="230" alt="A picture showing the mantis shrimp's body, it is long and looks like a lobster's tail with the same texture.">
<img src="mantisclose.jpg" width="300" alt="A picture close up to a mantis shrimp's face, you can see their large eyes and their 'mouth'.">
</div>
<div id="clickToShow"></div>
<script src="imagetotext.js"></script>
</body>
</html>
Here is my JavaScript
let altText = document.getElementById('clickToShow');
let clicker = document.getElementById('showAlt');
for (let x = 0; x < document.images.length; x++) {
altText[x].addEventListener('click', imageToText);
}
function imageToText() {
let clickImage = this.alt;
clicker.textContent = clickImage;
}
I was trying to add a for loop each time the user clicked the image, it shows its alt text but I keep getting errors saying that it cannot read property addEventListener of undefined imageToText. I thought it would be that the JS was too high up in the HTML since it is undefined, but even after moving my script tag down this is still happening. I also want to use the event listener tag--I haven't learned jQuery yet. Any help would be great! I'm feeling stuck with my code at the moment.