views:

40

answers:

2

Essentially i'm trying to get the value 'col' that is set in the html, and use it as the value for backgroundColor.

Here's the script:

function bgc(col) {
        $("#BG") .animate({ backgroundColor: "col" }, 1000)
}

Here's the html:

<div id="BG">
    <a href="#" onclick="bgc(#ffffff);"></a>
</div>

Any help would be great, i've been fiddling for a while but i can't quite grasp it. Thanks

+2  A: 

Try this:

function bgc(col) {
        $("#BG") .animate({ backgroundColor: col }, 1000)
}

and

<div id="BG">
    <a href="#" onclick="bgc('#ffffff');"></a>
</div>
Jacob Relkin
+1  A: 

You're quoting it in the wrong place:

function bgc(col) { 
        $("#BG") .animate({ backgroundColor: col }, 1000) 
} 


<div id="BG"> 
    <a href="#" onclick="bgc('#ffffff');"></a> 
</div>

On a further note, a better practice is to keep your markup and javascript separate. Note how this gracefully degrades if javascript is note enabled.

$(function() {
    $('#BG').find('a').show().click( function() {
         $('#BG').animate( { backgroundColor: '#ffffff' }, 1000 );
    });
});

<div id="BG">
    <a href="#" style="display: none;"></a>
</div>
tvanfosson
I'm using a whole list of links, and i'm setting the color for each link, i can do this no problem by writing out each function for each link with the color in the script, but it's pointless replication of the code, and doesn't solve what i'm trying to do.
shovelshed
@shovelshed -- could you use classes for this? Assign a class to each category of link, then set up handlers based on the classes?
tvanfosson