Mouseover Javascript

Matthieu

I have this HTML :

<div class="firstScene">
    <div class="text"> <div class="welcomeMessage">iPhone X</div> </div>
    <div class="pictures">
        <img src="assets/img/iphoneX_off.png" alt="iphone X" id="iphoneX">
        <img src="assets/img/iphoneX_screen.png" alt="screen on" id="iphoneScreen">
    </div>
</div>

and this javascript :

function initHover () {
  var screen = document.getElementsById("iphoneScreen")

  screen.onmouseover = mouseOver
  screen.onmouseout = mouseOut
}


function mouseOver() {
  var welcomeMessage = document.getElementsByClassName("welcomeMessage")
  welcomeMessage.className += "screenOn"
}

I want to add the class "screenOn" on the div with the class "welcomeMessage" but it doesn't work. May someone help me ?

PS : I don't have the right to use jQuery...

Krusader

I've fixed your js code so try to use it know, should work

JS:

function initHover () {
  var screen = document.getElementById("iphoneScreen");

   screen.onmouseover = mouseOver
   screen.onmouseout = mouseOut
}


function mouseOver() {
   var welcomeMessage = document.querySelector(".welcomeMessage");
   welcomeMessage.className += " screenOn";
}

Note in mouseOver function I've changed getElementsByClassName, that you used, to the querySelector that way you get single object instead of the live HTMLCollection that returned from the getElementsByClassName

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

javascript mouseover and out functions

JavaScript pause setInterval on mouseover

Implementing a mouseover function in JavaScript

Javascript Mouseover with Image

Show picture on mouseover with javascript

Changing images with mouseover in Javascript

Javascript mouseover event delegation

Javascript Text following the cursor on mouseover

Table Row mouseover effect with Javascript

JavaScript / jQuery mouseover event with delay

CSS hover vs. JavaScript mouseover

Javascript doesn't respond to mouseover a hyperlink

Javascript: Zoom in on mouseover WITHOUT Jquery or plugins

html javascript expand <details> on mouseover and collapse on mouseout

Replacing images on mouseover with only childnodes and javascript

include one mouseover into a div created with javascript

JavaScript: keep mouseover until another event triggers

MouseOver CSS3D effect with javascript

Change color of an element in JavaScript with mouseover event

Stuck on Some Javascript - Displaying Image on Mouseover of text

Javascript mouseover run function, then wait and run again

Javascript: play() and pause() html video not work with mouseover

JavaScript mouseover event vs click event

How to implement mouseover event in Javascript inside of Jmeter?

Adding mouseover event to a javascript created DIV

Javascript populating hidden fields on mouseover - Before click

Conflicting javascript or too many mouseover events?

How to disable mouseover event from everywhere using javascript/jquery?

JavaScript MouseOver is only working for the first instance in the Velocity #foreach statement