I am looking to dynamically change the background image of my body div whenever a link is hovered over. Here is a snippet of my code:
JAVASCRIPT:
var i = 0,
anchors = document.querySelectorAll("zoom"),
background = document.getElementById("body");
function backgroundChange() {
"use strict";
window.console.log("hey");
if (event.target.id === "4Mo3We2Days") {
background.style.background = "url('../Images/movies/4Mo3We2Days.png')";
...
other specific ids
...
}
}
function backgroundRemove() {
"use strict";
background.style.backgroundImage = "none";
}
for (i = 0; i < anchors.length; i += 1) {
anchors[i].addEventListener("mouseover", backgroundChange, false);
anchors[i].addEventListener("mouseout", backgroundRemove, false);
window.console.log("yo");
}
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "../CSS/JMWStyles.css">
<title>Archives</title>
<meta charset="UTF-8">
</head>
<body>
<header>
<img id = "logo" src = "../Images/logo.png" alt = "logo" href = "../JMW.htm"></img>
<ul id = "navMenu">
<li class = "nav"><a id = "button5" class = "navItem" href = "../JMW_Store.htm">Store</a></li>
...
other nav items
...
</ul>
</header>
<hr>
<div>
** Please Note that these were written for the radio and were meant to be read aloud. There are many typos as
well as odd things to read. If you would like, leave a comment so I can fix it! <br>
  - Jake Winters
</div>
<hr>
<div id = "body">
<ul class = "centered">
<li><a class = "zoom" id = "4Mo3We2Days" href = "MovieReviews/4Mo3We2Days.htm">4 Months 3 Weeks and 2 Days</a></li>
...
other movie links
...
</ul>
</div>
<footer>
<hr id = "footerHr">
<ul id = "socialMenu">
<li class = "social"><a href = "spotify:user:1256028498"><img class = "footerImg" id = "spotify" src = "../Images/spotify.png" alt = "spotify"></a></img></li>
...
other social media links
...
</ul>
</footer>
</body>
<script src = "../JS/JMW.js"></script>
<script src = "../JS/archive.js"></script>
</html>
CSS:
#body {
background-image: none;
background-repeat: no-repeat;
background-position: center;
}
I am uncertain if I am attaching the event listeners correctly. How can I determine the element that triggered the event within my defined functions? Is event.target.id the right approach?
I am trying to avoid using jQuery as I want to improve my understanding of basic JavaScript. However, if jQuery would be more suitable for this task, please explain why. Any guidance would be greatly appreciated. Thank you!
Additionally, I am considering styling the background images with a white rounded vignette and adjusting the alpha for consistency, but this is a secondary concern.