-1

I've got this piece of code, I'm generating a random color and converting it to HEX, then I want to set it as backgroundColor of the .ribbon a:hover span class:

<script type="text/javascript">
$(function() {

    var randomColor = Math.floor(Math.random()*16777215).toString(16);
    alert(randomColor);
    $(".ribbon a:hover span").css({

        backgroundColor: '#' + randomColor

    });



});
</script>

Here's my css:

.ribbon a:hover span {
    background: /*<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>*/ #FFF;
    margin-top:0;
}

It doesn't even alert my randomColor variable... I've put this script before the </body> tag...

Alexander
  • 23,432
  • 11
  • 63
  • 73
moritzg
  • 4,266
  • 3
  • 37
  • 62

3 Answers3

4

jQueryUI doesn't include jQuery : you still need to load it (before).

So you should replace

<script src="http://www.google.com/jsapi"></script>
<!-- load JQuery and UI from Google (need to use UI to animate colors) -->
<script type="text/javascript"> google.load("jqueryui", "1.5.2"); </script>

with

<script src="http://www.google.com/jsapi"></script> <!-- load JQuery and UI from Google (need to use UI to animate colors) -->
<script type="text/javascript">
google.load("jquery", "1.5.2");
google.load("jqueryui", "1.5.2");
</script>
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    As an aside, you probably want a more recent version of jQuery than 1.5.2. Also, once OP fixes his jQuery loading problem, there's still the nonsense (to jQuery) selector `$(".ribbon a:hover span")`. – thirtydot Nov 23 '12 at 21:34
  • Hello, the imports worked! But now how do I set the background color of my class? – moritzg Nov 23 '12 at 21:44
1

First, you'll have to load jQuery - the code you provided only loads jQuery UI.

<script src="http://www.google.com/jsapi"></script>
<!-- load JQuery and UI from Google (need to use UI to animate colors) -->
<script type="text/javascript">
    google.load("jquery", "1.5.2");
    google.load("jqueryui", "1.5.2");
</script>

Second, you can't bind a function to the :hover selector like that, you need to use the jQuery function .hover:

<script type="text/javascript">
$(function() {


    $(".ribbon a").hover(function() {
        // change to random color on mouseover
        var randomColor = Math.floor(Math.random()*16777215).toString(16);
        alert(randomColor);
        $(this).find('span').css({
            backgroundColor: '#' + randomColor
        });
    }, function() {
        // change back to original color on mouseout
        $(this).find('span').css({
            backgroundColor: '#FFF'
        });
    });
});
</script>
freejosh
  • 11,263
  • 4
  • 33
  • 47
0

Either you're not importing jQuery, or you have JS errors elsewhere in your code that is causing your script(s) as a whole to break. First, check your console for any errors. If that doesn't reveal anything, try changing your script to this:

<script type="text/javascript">
    $(function(){
        alert('jQuery is alive!');
    });
</script>
Madbreaks
  • 19,094
  • 7
  • 58
  • 72