Image source related coding questions - SheCodes Athena - | SheCodes

Image source related coding questions

👩‍💻 HTML, CSS and JavaScript Code instructions

I want to change the src in img after clicking button with addEventListener with five different images. So when I click on the button five times, it clicks through all five of my images

🤖 SheCodes Athena says:

Answered in 8.25 seconds
html
Copy code
<!DOCTYPE html> <html> <head> <title>Change Image Source</title> <style> img { width: 300px; height: 200px; object-fit: cover; } button { margin-top: 10px; padding: 10px 20px; font-size: 16px; } </style> </head> <body> <img id="myImg" src="first_image.jpg" alt="Image"> <button id="myButton">Change Image</button> <script> const images = ['second_image.jpg', 'third_image.jpg', 'fourth_image.jpg', 'fifth_image.jpg']; const imgElement = document.getElementById('myImg'); const button = document.getElementById('myButton'); let imgIndex = 1; button.addEventListener('click', () => { if (imgIndex < images.length) { imgElement.src = images[imgIndex]; imgIndex++; } else { imgIndex = 0; imgElement.src = images[imgIndex]; imgIndex++; } }); </script> </body> </html>

In this code, we have an img tag with an initial source attribute (src). There is a button that triggers the image change. Inside the <script> tag, we define an array images that holds the filenames of the five images you want to cycle through. We also get references to the img element and the button element using their respective ids. We then define a variable imgIndex with an initial value of 1. This variable keeps track of which image from the images array to show next.

In the event listener for the button's click event, we check if imgIndex is less than the length of the images array. If it is, we set the source attribute of the img element to the image at the imgIndex position in the images array, and then increment imgIndex by 1. If imgIndex is equal to the length of the images array, we reset imgIndex to 0 and set the source attribute of the img element to the first image in the images array.

This way, each time the button is clicked, the img element's source attribute is updated to the next image in the images array, and when it reaches the end, it starts from the beginning again.

👩‍💻 Woah thank you so much!!!!

🤖 SheCodes Athena says:

Answered in 1.35 seconds

You're welcome! I'm glad I could help. If you have any more questions, feel free to ask. Happy coding!

🤔 Frequently Asked Questions

If you have any other questions, you can easily reach out to us here

AI stands for Artificial Intelligence. AI bots are able to learn from conversations with users and expand their knowledge this way.

SheCodes Athena will help you with technical questions about your code using artificial intelligence to find the answer. Imagine a super powerful human who has memorized everything on the internet and can access that knowledge in a matter of seconds. 🤯

SheCodes Athena can answer most coding-related questions, even complicated ones! It can even find bugs in your code and tell you how to fix them in just a few seconds. Impressive, right?

Just remember we're still in testing mode so the AI may return strange or incorrect replies. Feel free to message us if this happens!

SheCodes Athena can only reply to coding-related technical questions. The same type of questions you would ask in the channels on Slack.

For questions that are not coding-related, write us here 😃

You should treat Athena like a SheCodes team member, so always be polite! 😊 Ask your questions as detailed as possible, just like you would do on Slack.

Here are some examples:

- Prettier isn't working on my VS Code. How do I fix this?

- How do I make bullet points with different colors using the list element?

- My code in Codesandbox is having some issues. Can you please tell me what the issue is? [Include the link to your Codesandbox]

For now, SheCodes Athena is limited to 5 questions per day for each student.

In that case, you can either ask SheCodes Athena a follow-up question, or you can post on the designated weekly channel on Slack!

Our technical assistants are still available on Slack and are always happy to help! 😍💪

Remember, questions are limited to 1000 characters.

- If you're working with an HTML file: Post a snippet of your code related to the issue you're having (just copy the code and paste it into the question box).

- If you're working with Codesandbox: Good news, you can just post the link to your Codesandbox and the AI Assistant will be able to view your code.

- If you have a longer question that would require an entire HTML file or more than 1000 characters, post it in the designated weekly channels on Slack! 😃

Athena was the Greek goddess of wisdom, among other elements. She received her name from the city of Athens, which she is known for protecting.

Much like the goddess Athena, SheCodes Athena is also incredibly wise and can solve complicated coding puzzles in a matter of seconds! 😍

Not likely. AI can automate tasks and make developers' jobs more efficient but it can't fully replace the human ability to deal with complex software. And AI will still require human developers to supervise and improve it further.

So developers may see their tasks change but they won't be replaced by AI. 👩‍💻🤝💻