views:

72

answers:

1

How do i convert 5 click functions into one? eg

$("div#styleswitcher a.green").click(function(){
$("container").css({'background' : 'green'});

)

$("div#styleswitcher a.blue").click(function(){
$("container").css({'background' : 'blue'});

)

$("div#styleswitcher a.red").click(function(){
$("container").css({'background' : 'red'});

)

$("div#styleswitcher a.yellow").click(function(){
$("container").css({'background' : 'yellow'});

)
+6  A: 

This comes to mind:

$("#styleswitcher a").click(function ()
{
    $("container").css("background-color", this.className);
});

Assumption is that the background color should be the class name, as in your example

Philippe Leybaert
Good and clean solution!
tekBlues