FAQ: DOM Events with JavaScript - Mouse Events

This community-built FAQ covers the “Mouse Events” exercise from the lesson “DOM Events with JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Building Interactive JavaScript Websites

FAQs on the exercise Mouse Events

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there.

I’ve fallen at the first hurdle here.
Question one.

What am I doing wrong?

// These variables store the boxes on the side
let itemOne = document.getElementById('list-item-one');
let itemTwo = document.getElementById('list-item-two');
let itemThree = document.getElementById('list-item-three');
let itemFour = document.getElementById('list-item-four');
let itemFive = document.getElementById('list-item-five');
let resetButton = document.getElementById('reset-button');

// This function programs the "Reset" button to return the boxes to their default styles
let reset = function() {
  itemOne.style.width = ''
  itemTwo .style.backgroundColor = ''
  itemThree.innerHTML = 'The mouse must leave the box to change the text'
  itemFive.hidden = true;
};
resetButton.onclick = reset;

// Write code for the first list item
itemOne.mouseover

// Write code for the second list item


// Write code for the third list item


// Write code for the fourth list item


3 Likes

Referring to Exercise #3, an event handler property consists of:

  • the event target (e.g. itemOne)
  • followed by the event name; i.e. the event type with the prefix ‘on’ (e.g. onmouseover)
5 Likes

Hi Team,

I keep getting itemOne is not defined error. This is for the second question in the exercise to:

Now, assign an anonymous event handler function that changes the width of itemOne to any size greater or less than 400px .

My code is here:
let itemOne = document.getElementById(‘list-item-one’); (provided by the course).

itemOne.onmouseover = function() {
itemOne.style.width > “400px”
};

Any ideas why team?

1 Like

Figured it out - set it exactly to equal another value literally (500px ) or 300px.

Does anyone know there is a better way to code this? or do we have to be explicit in events to exact sizes?

1 Like

I first had issues when trying the “mouseover” and noticed you had to use ONmouseover when doing the itemOne.onmouseover; , but then i had som issues with step 2. I noticed though that it works fine when using the addEventListener. So if you are having issues, try to use itemOne.addEventListener(‘mouseover’, function() {});
And notice that when using the addEventListener you are NOT using ONmouseover, but only mouseover.

3 Likes

What’s the difference between event handler and event Listener ?

1 Like

A listener, well, listens. A handler, handles.

Listeners are attached to DOM nodes (or the document object, itself) and are used to detect a particular event such as a click or a mouse movement (or a key press). They are methods of the window object.

A handler is a function we write and assign to the listener which will invoke it on a triggered event.

function handler(event) {
    // action on event target node, perhaps?
}

domnode.onclick = handler

 node   method    function
2 Likes

For step 2 make sure to put your value in quotations.
With this syntax:

eventTarget.style.width = 'value';

Word value above should be replaced by the actual value that you want to give the element.

7 Likes

I don’t understand the way information is presented in this lesson. It feels like someone is addressing me like they would a toddler, only they don’t have kids so they use too many unfamiliar words, and forget to share half the information.

Instruction 2: Now, assign an anonymous event handler function that changes the width of itemOne to any size greater or less than 400px .

Why? How much? Where? I just wrote

Instruction 1: First, create an event handler property on itemOne when the mouse hovers over it.

What I’m reading is that, although I just assigned an onmouseover to itemOne, I now have to add an eventlistener for the event inside the eventHandler. What I’m reading is that I’m supposed to do his:

itemOne.onmouseover = function () {
  itemOne.addEventListener("mouseover", ?????)
}

So my question is: how am I supposed to interpret these instructions? Can someone word it differently for me?

2 Likes

I have another thing: if I reset the workspace, I start with a red cross through the first instruction’s checkbox, and the error pop-upmessage. It seems not to entirely reset.

Update: when I go to the previous page, and then come back, the exercise is completely reset.

Replying to my own post because I figured out the correct code:
This is the answer:

itemOne.onmouseover = () => { itemOne.style.width = "300px"; }

So the confusion between listeners and handlers is cleared up somewhat, also thanks to the earlier reply of @mtf

I do think there may be a bug in the first two assignments. I wrote this answer before, as one of the attempts before I reset the lesson entirely (see my question below), and then it was not approved as correct.
After the reset, it was.

I’m not happy!

I’m the opposite to everyone here!! on task 2 it’s asked us to change itemOne to any size greater than 400px. At first I’ve passed it straight away thinking I’ve understood but then I’ve reseted the code just to make sure and it’s turned out that it’ll pass me regardless!! It even passes me when I didn’t even type in any code! NOT HAPPY!! I’m here to learn! :weary:

1 Like

OK, cAcademy really needs to fix this issues! All the way up to task 5 it’ll let you pass the test no matter what you code and luckily I have the patients and willing to learn so I’ve managed to practice and understood how to do those coding myself as I can see the width has change, the colour has changed when associated it with events.

This is no good as I could of just pass through it without knowing that I wasn’t learning anything!

3 Likes

I had a problem with task 2, and most likely people in future would have problem with this same thing because the actual error is very obscure for us to notice.

assign an anonymous event handler function that changes the width of itemOne to any size greater or less than 400px

And we assign it as a number which causes the problem, we should put any value either less or greater than 400px inside a pair of quote marks like this ‘300px’.
Hope this helps

3 Likes

I can’t seem to get past task five

5.
Next, create an event handler property that fires when the mouse leaves the itemThree element

I’ve entered in: itemThree.onmouseout but it’s not liking it.

edit: I clicked on the solve button and the answer is the same:

itemThree.onmouseout

I’m sure you solved this by now, but for anyone else, I found I had to fill in the empty function for it to pass:

itemThree.onmouseout = () => {};
3 Likes
let itemOne = document.getElementById('list-item-one');
let itemTwo = document.getElementById('list-item-two');
let itemThree = document.getElementById('list-item-three');
let itemFour = document.getElementById('list-item-four');
let itemFive = document.getElementById('list-item-five');
let resetButton = document.getElementById('reset-button');

// This function programs the "Reset" button to return the boxes to their default styles
let reset = function() {
  itemOne.style.width = ''
  itemTwo .style.backgroundColor = ''
  itemThree.innerHTML = 'The mouse must leave the box to change the text'
  itemFive.style.display = "none"
};
resetButton.onclick = reset;

What confuses me lately is why are we using let when declaring functions.
From my understanding from previous JS lessons is that if we don’t intend on changing the variable later we should assign it using const

myCode

1 Like

Thank you so much. I have no idea how Codecademy expected us to know that. This section is so miserable.

1 Like

Many typo’s in this lessons ‘Instructions’ sections, and some problems with mismatches between the already provided code and the expected code in the tester functions. Please review this whole lesson as parts of it are currently impossible to pass without taking so much initiative as to rewrite the already provided code we’re supposed to build off to complete the exercises? Very frustrating.

1 Like