FAQ: Style Methods - .css()

This community-built FAQ covers the “.css()” exercise from the lesson “Style Methods”.

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

Web Development

Introduction to jQuery

FAQs on the exercise .css()

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!

Are we, in a similar context as this exercise, also allowed to leave the value of the css color property empty when we would like to reset it to the original value?

So…

$(‘.nav-menu’).on(‘mouseleave’, () => {
$(‘.nav-menu’).hide();
$(“.menu-button”).css(“color”, “”);
})

Instead of:

$(‘.nav-menu’).on(‘mouseleave’, () => {
$(‘.nav-menu’).hide();
$(“.menu-button”).css(“color”, “#EFEFEF”);
})

Well, kind of. It depends on what you mean by “original value”.

I have prepared a simple example, take a look -> https://repl.it/@factoradic/Reseting-color-property-value

We have two paragraphs, #first has color set to blue in the stylesheet, #second has color set to blue in the inline style. In the script we change the colors of both elements and we create an event listener that will change color to an empty string on mouseenter event.

When we hover the mouse over the first paragraph its color gets changed to the blue. Ok, that is what we expected.

But when we hover the mouse over the second paragraph its color changes to (probably) black. Why? Because when we change the color property via JavaScript what we actually do is overriding inline style. So information about the previous color of the #second (blue) is simply gone.

So if you want to preserve the previous color and later “reset it” the best way to accomplish this is to store the current color in a variable. And after all the modifications you will be able to set the color property of a given element to this saved value.

2 Likes

Below should be an acceptable if not the only answer. Why are you forcing us to rewrite the event listener for the menu-button on mouse enter instead of using the one that is already written? D.R.Y.

$(document).ready(() => {

('.login-button').on('click', () => { (’.login-form’).toggle();
});

('.menu-button').on('mouseenter', (event) => { (’.nav-menu’).show();
$(event.currentTarget).css(‘color’, ‘#C3FF00’);
})

('.nav-menu').on('mouseleave', () => { (’.nav-menu’).hide();
$(’.menu-button’).css(‘color’, ‘#EFEFEF’);
})

});