Skip to content
Advertisement

How to change background of div using button

I have code:

JavaScript

How can I change background color of <div class="block"> using buttons <input type="button" name="grey" value="grey"> and <input type="button" name="red" value="red">? I tried to do it, but nothing helps. Who knows? P.S.: it doesn’t matter, what to use (PHP or JS).

Advertisement

Answer

for do that you can use javascript. I suggest to change the class of the div when button is clicked and add the good rules in css, or just change style property of the element. But css is here for taht and it’s more clean to do that with classes and css. You have also to add an id to the button like #red or #blue Exemple with classes :

JavaScript

And it’s the contrary for the blue button

If you have several div,I think you can do that :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement