1

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?

2 Answers 2

4

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.

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

Comments

0
$('button').click(function()
{
    $('.toolbar').css('background-color', $(this).css('background-color'));
});

2 Comments

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.
I know it will. The questioner didn't provide good enough information for a good selector though.

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.