Jake Winters
Jake Winters

Reputation: 85

Im trying to handle a mouse over event with Javascript and having some issues

I want the background of my body div to change to a different image as each link is hovered over. Here is my code with the link section removed beside the first one as an example:

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>
                <li class = "nav"><a id = "button4" class = "navItem" href = "../JMW_Archives.htm">Archives</a></li>
                <li class = "nav"><a id = "button3" class = "navItem" href = "../JMW_About.htm">About</a></li>
                <li class = "nav"><a id = "button2" class = "navItem" href = "../JMW_Blog.htm">Blog</a></li>
                <li class = "nav"><a id = "button1" class = "navItem" href = "../JMW.htm">Home</a></li>
            </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>
            &emsp; - 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>
                <li><a class = "zoom" id = "aMostViolentYear" href = "MovieReviews/aMostViolentYear.htm">A Most Violent year</a></li>
                <li><a class = "zoom" id = "pigeon" href = 
                       "MovieReviews/A%20Pigeon%20Sat%20on%20a%20Branch%20Reflecting%20on%20Existence.htm">A Pidgeon Sat
                       on a Branch Reflecting on Existence</a></li>
                <li><a class = "zoom" id = "akira" href = "MovieReviews/akira.htm">Akira</a></li>
                <li><a class = "zoom" id = "amadeus" href = "MovieReviews/Amadeus.htm">Amadeus</a></li>
                <li><a class = "zoom" id = "borgman" href = "MovieReviews/Borgman.htm">Borgman</a></li>
                <li><a class = "zoom" id = "briefInterviews" href = "MovieReviews/briefInterviewsWithHideousMen.htm">Brief Interviews with Hideous Men</a></li>
                <li><a class = "zoom" id = "bronson" href = "MovieReviews/bronson.htm">Bronson</a></li>
                <li><a class = "zoom" id = "catchMeDaddy" href = "MovieReviews/catchMeDaddy.htm">Catch Me Daddy</a></li>
                <li><a class = "zoom" id = "copCar" href = "MovieReviews/copCar.htm">Cop Car</a></li>
                <li><a class = "zoom" id = "gangsOfNY" href = "MovieReviews/gangsOfNewYork.htm">Gangs of New York</a></li>
                <li><a class = "zoom" id = "gattaca" href = "MovieReviews/gattaca.htm">Gattaca</a></li>
                <li><a class = "zoom" id = "hailCeaser" href = "MovieReviews/hailCeaser.htm">Hail Ceaser</a></li>
                <li><a class = "zoom" id = "hidden" href = "MovieReviews/hidden.htm">Hidden</a></li>
                <li><a class = "zoom" id = "hudsucker" href = "MovieReviews/theHudsuckerProxy.htm">The Hudsucker Proxy</a></li>
                <li><a class = "zoom" id = "inBruges" href = "MovieReviews/inBruges.htm">In Bruges</a></li>
                <li><a class = "zoom" id = "koyscatsi" href = "MovieReviews/koyaanisqatsi.htm">Koyaanisqatsi</a></li>
                <li><a class = "zoom" id = "leviathan" href = "MovieReviews/leviathan.htm">Leviathan</a></li>
                <li><a class = "zoom" id = "libre" href = "MovieReviews/libreEtAssoupi.htm">Libre et Assoupi</a></li>
                <li><a class = "zoom" id = "moon" href = "MovieReviews/moon.htm">Moon</a></li>
                <li><a class = "zoom" id = "mustang" href = "MovieRevies/mustang.htm">Mustang</a></li>
                <li><a class = "zoom" id = "niceGuys" href = "MovieReviews/niceGuys.htm">Nice Guys</a></li>
                <li><a class = "zoom" id = "onlyGodForgives" href = "MovieReviews/onlyGodForgives.htm">Only God Forgives</a></li>
                <li><a class = "zoom" id = "pansLab" href = "MovieReviews/pansLabyrinth.htm">Pans Labyrinth</a></li>
                <li><a class = "zoom" id = "planesTrains" href = "MovieReviews/planesTrainsAndAutomobiles.htm">Planes, Trains, and Automobiles</a></li>
                <li><a class = "zoom" id = "psycho" href = "MovieReviews/psycho.htm">Psycho</a></li>
                <li><a class = "zoom" id = "repoman" href = "MovieReviews/repoman.htm">Repoman</a></li>
                <li><a class = "zoom" id = "revenant" href = "MovieReviews/revenant.htm">Revenant</a></li>
                <li><a class = "zoom" id = "sevenPsycho" href = "MovieReviews/sevenPsychopaths.htm">Seven Psychopaths</a></li>
                <li><a class = "zoom" id = "seven" href = "MovieReviews/seven.htm">Seven</a></li>
                <li><a class = "zoom" id = "stillWalking" href = "MovieReviews/stillWalking.htm">Still Walking</a></li>
                <li><a class = "zoom" id = "theFifthElement" href = "MovieReviews/theFifthElement.htm">The Fifth Element</a></li>
                <li><a class = "zoom" id = "theIllusionist" href = "MovieReviews/theIllusionist.htm">The Illusionist</a></li>
                <li><a class = "zoom" id = "thePast" href = "MovieReviews/thePast.htm">The Past</a></li>
                <li><a class = "zoom" id = "theSurvivalist" href = "MovieReviews/theSurvivalist.htm">The Survivalist</a></li>
                <li><a class = "zoom" id = "theUsualSuspects" href = "MovieReviews/theUsualSuspects.htm">The Usual Suspects</a></li>
                <li><a class = "zoom" id = "theyLive" href = "MovieReviews/theyLive.htm">They Live</a></li>
                <li><a class = "zoom" id = "thisIsNotAFilm" href = "MovieReviews/thisIsNotAFilm.htm">This is not a Film</a></li>
                <li><a class = "zoom" id = "turboKid" href = "MovieReviews/turboKid.htm">Turbo Kid</a></li>
                <li><a class = "zoom" id = "weirdScience" href = "MovieReviews/weirdScience.htm">Weird Science</a></li>
                <li><a class = "zoom" id = "whatWeDoInShadows" href = "MovieReviews/whatWeDoInTheShadows.htm">What We do in the Shadows</a></li>
                <li><a class = "zoom" id = "waltzWithB" href = "MovieReviews/waltzWithBashir.htm">Waltz With Bashir</a></li>
                <li><a class = "zoom" id = "garp" href = "MovieReviews/worldAccordingToGarp.htm">World According to Garp</a></li>
                <li><a class = "zoom" id = "worldsGreatestDad" href = "MovieReviews/worldsGreatestDad.htm">World's Greatest Dad </a></li>
            </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>
                <li class = "social"><a href = "https://www.instagram.com/jake_m_winters/"><img class = "footerImg" id = "insta" src = "../Images/insta.png" alt = "insta"></img></a></li>
                <li class = "social"><a href = "https://www.linkedin.com/in/JacobMWinters"><img class = "footerImg" id = "linkedIn" src = "../Images/linkedin.png" alt = "linkedIn"></img></a></li>
                <li class = "social"><a href = "https://soundcloud.com/user-311952172"><img class = "footerImg" id = "soundCloud" src ="../Images/soundcloud.png"></img></a></li>
            </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;
}

am I adding the listeners right? How do I know from within the functions I have defined what the element that made the event happen is? is event.target.id right?

I have tried a few methods but have found many ways of doing so. I don't want to use jQuery as I am trying to learn base javascript. If you feel like this is really better suited to jQuery please let me know why. Any help is very appreciated. Thanks!

Another side question that I only want an answer to if you know of the top of your head: I want to style the background images I use to all have a white rounded vignette so the belend into the background. I also want the alpha of all of the images changed. Once again this isn't my main question and can be ignored

Upvotes: 0

Views: 48

Answers (2)

Scott Marcus
Scott Marcus

Reputation: 65806

The biggest issue was your querySelectorAll("zoom"), which needs to be querySelectorAll(".zoom") since you are looking for classes. As such, you were never successfully getting access to any of the links you wanted to hook up to event handlers.

You also had some invalid HTML (<img> elements don't get a closing tag).

The event object is automatically passed to event handling functions as the first argument, so that is the standard way to access it. Just accessing event (as a global) is specific to IE and would also prevent you from accessing the event.target in some browsers. As I stated in the comments, this is another way to reference the DOM object that invoked the event, so this.id would get you the same object reference as event.target.id.

Lastly, I recommend you change some of your identifier names (body shouldn't really be an id for anything but the <body> element).

I added instructions to change the color of the section in question when the mouse moves in and out of it just for demonstration here.

See the inline comments below:

var i = 0,
// You are looking for classes, so your selector needs to start with a "."
anchors = document.querySelectorAll(".zoom"),
body = document.getElementById("main");

// Every event callback function will automatically 
// be passed a reference to the event it is responding
// to. Access that event by setting up a callback
// argument (evt here). Accessing the event via "event" 
// is specific to older versions of Internet Explorer.
function backgroundChange(evt) {
  "use strict";

    console.log("hey");

    // evt.target references the same thing as "this" in this case
    if (this.id === "4Mo3We2Days") {
        body.style.background = "url('../Images/movies/4Mo3We2Days.png')";
        
        // This is just for testing
        body.style.backgroundColor = "yellow";
        //other specific ids
    }
}

function backgroundRemove() {
    "use strict";
    body.style.backgroundImage = "none";
    
    // Just for testing
    body.style.backgroundColor = "initial";
}

for (i = 0; i < anchors.length; i += 1) {
    // .addEventListener uses false as the default for the third argument, so you don't have to add it
    anchors[i].addEventListener("mouseover", backgroundChange);
    anchors[i].addEventListener("mouseout", backgroundRemove);
}
#main {
    background-image: none;
    background-repeat: no-repeat;
    background-position: center;
}
<header>
   <!-- img elements don't get closed, there is no such thing as </img> -->
   <img id="logo" src = "../Images/logo.png" alt = "logo" href = "../JMW.htm">
   <ul id="navMenu">
     <li class="nav"><a id = "button5" class = "navItem" href = "../JMW_Store.htm">Store</a></li>
     <li class="nav"><a id = "button4" class = "navItem" href = "../JMW_Archives.htm">Archives</a></li>
     <li class="nav"><a id = "button3" class = "navItem" href = "../JMW_About.htm">About</a></li>
     <li class="nav"><a id = "button2" class = "navItem" href = "../JMW_Blog.htm">Blog</a></li>
     <li class="nav"><a id = "button1" class = "navItem" href = "../JMW.htm">Home</a></li>
   </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>
    &emsp; - Jake Winters
</div>

<hr>

<!-- Don't identify something that isn't the <body> as body. It's confusing -->
<div id="main">
  <ul class="centered">
    <li><a class = "zoom" id = "4Mo3We2Days" href = "MovieReviews/4Mo3We2Days.htm">4 Months 3 Weeks and 2 Days</a></li>
    <li><a class = "zoom" id = "aMostViolentYear" href = "MovieReviews/aMostViolentYear.htm">A Most Violent year</a></li>
    <li><a class = "zoom" id = "pigeon" href = 
                       "MovieReviews/A%20Pigeon%20Sat%20on%20a%20Branch%20Reflecting%20on%20Existence.htm">A Pidgeon Sat
                       on a Branch Reflecting on Existence</a></li>
    <li><a class = "zoom" id = "akira" href = "MovieReviews/akira.htm">Akira</a></li>
    <li><a class = "zoom" id = "amadeus" href = "MovieReviews/Amadeus.htm">Amadeus</a></li>
    <li><a class = "zoom" id = "borgman" href = "MovieReviews/Borgman.htm">Borgman</a></li>
    <li><a class = "zoom" id = "briefInterviews" href = "MovieReviews/briefInterviewsWithHideousMen.htm">Brief Interviews with Hideous Men</a></li>
    <li><a class = "zoom" id = "bronson" href = "MovieReviews/bronson.htm">Bronson</a></li>
    <li><a class = "zoom" id = "catchMeDaddy" href = "MovieReviews/catchMeDaddy.htm">Catch Me Daddy</a></li>
    <li><a class = "zoom" id = "copCar" href = "MovieReviews/copCar.htm">Cop Car</a></li>
    <li><a class = "zoom" id = "gangsOfNY" href = "MovieReviews/gangsOfNewYork.htm">Gangs of New York</a></li>
    <li><a class = "zoom" id = "gattaca" href = "MovieReviews/gattaca.htm">Gattaca</a></li>
    <li><a class = "zoom" id = "hailCeaser" href = "MovieReviews/hailCeaser.htm">Hail Ceaser</a></li>
    <li><a class = "zoom" id = "hidden" href = "MovieReviews/hidden.htm">Hidden</a></li>
    <li><a class = "zoom" id = "hudsucker" href = "MovieReviews/theHudsuckerProxy.htm">The Hudsucker Proxy</a></li>
    <li><a class = "zoom" id = "inBruges" href = "MovieReviews/inBruges.htm">In Bruges</a></li>
    <li><a class = "zoom" id = "koyscatsi" href = "MovieReviews/koyaanisqatsi.htm">Koyaanisqatsi</a></li>
    <li><a class = "zoom" id = "leviathan" href = "MovieReviews/leviathan.htm">Leviathan</a></li>
    <li><a class = "zoom" id = "libre" href = "MovieReviews/libreEtAssoupi.htm">Libre et Assoupi</a></li>
    <li><a class = "zoom" id = "moon" href = "MovieReviews/moon.htm">Moon</a></li>
    <li><a class = "zoom" id = "mustang" href = "MovieRevies/mustang.htm">Mustang</a></li>
    <li><a class = "zoom" id = "niceGuys" href = "MovieReviews/niceGuys.htm">Nice Guys</a></li>
    <li><a class = "zoom" id = "onlyGodForgives" href = "MovieReviews/onlyGodForgives.htm">Only God Forgives</a></li>
    <li><a class = "zoom" id = "pansLab" href = "MovieReviews/pansLabyrinth.htm">Pans Labyrinth</a></li>
    <li><a class = "zoom" id = "planesTrains" href = "MovieReviews/planesTrainsAndAutomobiles.htm">Planes, Trains, and Automobiles</a></li>
    <li><a class = "zoom" id = "psycho" href = "MovieReviews/psycho.htm">Psycho</a></li>
    <li><a class = "zoom" id = "repoman" href = "MovieReviews/repoman.htm">Repoman</a></li>
    <li><a class = "zoom" id = "revenant" href = "MovieReviews/revenant.htm">Revenant</a></li>
    <li><a class = "zoom" id = "sevenPsycho" href = "MovieReviews/sevenPsychopaths.htm">Seven Psychopaths</a></li>
    <li><a class = "zoom" id = "seven" href = "MovieReviews/seven.htm">Seven</a></li>
    <li><a class = "zoom" id = "stillWalking" href = "MovieReviews/stillWalking.htm">Still Walking</a></li>
    <li><a class = "zoom" id = "theFifthElement" href = "MovieReviews/theFifthElement.htm">The Fifth Element</a></li>
    <li><a class = "zoom" id = "theIllusionist" href = "MovieReviews/theIllusionist.htm">The Illusionist</a></li>
    <li><a class = "zoom" id = "thePast" href = "MovieReviews/thePast.htm">The Past</a></li>
    <li><a class = "zoom" id = "theSurvivalist" href = "MovieReviews/theSurvivalist.htm">The Survivalist</a></li>
    <li><a class = "zoom" id = "theUsualSuspects" href = "MovieReviews/theUsualSuspects.htm">The Usual Suspects</a></li>
    <li><a class = "zoom" id = "theyLive" href = "MovieReviews/theyLive.htm">They Live</a></li>
    <li><a class = "zoom" id = "thisIsNotAFilm" href = "MovieReviews/thisIsNotAFilm.htm">This is not a Film</a></li>
    <li><a class = "zoom" id = "turboKid" href = "MovieReviews/turboKid.htm">Turbo Kid</a></li>
    <li><a class = "zoom" id = "weirdScience" href = "MovieReviews/weirdScience.htm">Weird Science</a></li>
    <li><a class = "zoom" id = "whatWeDoInShadows" href = "MovieReviews/whatWeDoInTheShadows.htm">What We do in the Shadows</a></li>
    <li><a class = "zoom" id = "waltzWithB" href = "MovieReviews/waltzWithBashir.htm">Waltz With Bashir</a></li>
    <li><a class = "zoom" id = "garp" href = "MovieReviews/worldAccordingToGarp.htm">World According to Garp</a></li>
    <li><a class = "zoom" id = "worldsGreatestDad" href = "MovieReviews/worldsGreatestDad.htm">World's Greatest Dad </a></li>
  </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></li>
    <li class = "social"><a href = "https://www.instagram.com/jake_m_winters/"><img class = "footerImg" id = "insta" src = "../Images/insta.png" alt = "insta"></a></li>
    <li class = "social"><a href = "https://www.linkedin.com/in/JacobMWinters"><img class = "footerImg" id = "linkedIn" src = "../Images/linkedin.png" alt = "linkedIn"></a></li>
    <li class = "social"><a href = "https://soundcloud.com/user-311952172"><img class = "footerImg" id = "soundCloud" src ="../Images/soundcloud.png"></a></li>
  </ul>
</footer>

Upvotes: 0

trincot
trincot

Reputation: 350252

You did not select your anchors correctly. Since you target a class, you need to add the dot before zoom:

anchors = document.querySelectorAll(".zoom"),

Secondly, you need to pass the event argument for support in browsers other than IE:

function backgroundChange(event) {

Upvotes: 1

Related Questions