tags:

views:

51

answers:

2

I have a background colour set on a series of buttons, .button1, .button2., .button3 & .button4. Each of these buttons has a different background colour set in CSS.

I want to use jQuery to detect the background colour of the button when it is clicked and apply that colour to .toolbar.

Is this possible?

A: 
$('button').click(function()
{
    $('.toolbar').css('background-color', $(this).css('background-color'));
});
Coronatus
That will apply to all and any button on the form because you have used merely **button** selector which is not specific, might create problems if there are more buttons on the form from which he may not want to apply the background.
Sarfraz
I know it will. The questioner didn't provide good enough information for a good selector though.
Coronatus
+4  A: 

You could do:

$('button[class^="button"]').click(function(){
  $('.toolbar').css('background-color', $(this).css('background-color'));
});

This is generic and will automatically detect clicked button rather than writing code for each button having different classes. Also, this code makes sure that it does the stuff only for those buttons whose class names start with button.

Sarfraz