0

The following js code allows me, with buttons, to dynamically change color to the body class, it works great. However, in addition to backgroundColor I have tried to use other CSS rules, for example color, borderColor, on a button, and it works very well.

1) If I use box-shadow or boxShadow does not seem to work. Am I wrong or is there a list of CSS rules I can use?

2) $('h2').css('backgroundColor', '#000'); to get a change of color on mouse over I tried $('h2:hover').css('backgroundColor', '#ff0000'); but this does not work. Also in this case the reason is that dynamically you cannot use: hover? Or am I wrong?

$(document).ready(function () {

    var nightMode = function() {

            $('body').css('backgroundColor', '#000');   

        },
        normalMode = function() {
            $('body').css('backgroundColor', '#fff');

        };

    $("#night-mode").click(function () {
        localStorage.setItem('modalread', 'night-mode');
        nightMode();
    });

    $("#normal-mode").click(function () {
        localStorage.setItem('modalread', 'normal-mode');
        normalMode();
    });

    if (localStorage.getItem('modalread') == 'normal-mode') {
        normalMode();   
    } else if (localStorage.getItem('modalread') == 'night-mode') {
        nightMode();
    }

});
4
  • If I use box-shadow or boxShadow does not seem to work what doesn't work? how do you try to use it? Commented Oct 22, 2019 at 8:01
  • 2
    $('h2:hover') - unfortunately that's not how jQuery works Commented Oct 22, 2019 at 8:02
  • 1
    do not confuse jquery selectors with css selectors. They look similar but they are not the same. for hover jquery has a method called hover() . CHeck it out. Commented Oct 22, 2019 at 8:04
  • 1
    see Documentation (api.jquery.com/hover) about hover and change your code Commented Oct 22, 2019 at 8:06

1 Answer 1

2

You can work with css classes that you define:

define a class:

.fancy-class:hover {
    color: yellow !important;
}

With switching the colors run:

$("h2").addClass("fancy-class");

This $('h2:hover') is not a valid class selector. Thats why your solutions doesnt work. To make your code easier to maintain I would in general recommend for this to work with css classes

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.