1

Using CSS, I'm trying to set the background color of each element to a random color on hover:

:hover {
    background-color: "getRandom()";
}

However, it doesn't appear to be possible to put a JavaScript function call here. Is there any alternative approach that would work?

Here's the page I'm working on: http://jsfiddle.net/FwKqq/3/

2
  • You can't use JavaScript within CSS like that. What is your question? Commented Apr 20, 2013 at 4:15
  • @zzzzBov I'm trying to set an element's background color to the output of getRandom(), which returns a random color. Commented Apr 20, 2013 at 4:25

5 Answers 5

1

In your jQuery code:

$("*").hover(
    function(event) {
        $(this).css("background-color", getRandomColor());
    },
    function (event) {
        $(this).css("background-color", "white");
    }
);

(You should also remove the :hover css element)

Example: http://jsfiddle.net/jqSgq/

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

Comments

1

Try this

$(function() {
    $('*').hover(
        function() { $(this).css('background-color', getRandom()); }, 
        function() {$(this).css('background-color', '#FFF');}
    );
});

Comments

0
function changeBackground(color) {
   document.body.style.background = //here apply background colour;
}

call this function in hover event

Comments

0

Here is a working example: http://jsfiddle.net/FwKqq/4/

You need to set the background-color when the call starts and ends like this:

$("*").hover(
    function(event) {
        $(this).css('background-color', getRandomColor());
    },
    function (event) {
       $(this).css('background-color', 'white');
    }
 );

Comments

0

Pure cross-browser Javascript with working example:

var bgColor;
var els = document.getElementsByTagName('*');
for (var i = 0; i < els.length; i++) {
    if (document.addEventListener) {
        els[i].addEventListener('mouseover', function (e) {
            e.stopPropagation();
            bgColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
            this.style.backgroundColor = bgColor;
        }, false);
        els[i].addEventListener('mouseout', function (e) {
            e.stopPropagation();
            bgColor = '#FFFFFF';
            this.style.backgroundColor = bgColor;
        }, false);
    } else {
        els[i].attachEvent('mouseover', function () {
            e.stopPropagation();
            bgColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
            this.style.backgroundColor = bgColor;
        });
        els[i].attachEvent('mouseout', function () {
            e.stopPropagation();
            bgColor = '#FFFFFF';
            this.style.backgroundColor = bgColor;
        });
    }
}

Random background code from here: http://paulirish.com/2009/random-hex-color-code-snippets/

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.