Change background with js

0

Well, I have a demo image

link

When a user clicks on one, the background of the body will change according to what he has chosen. But in this case the background is an image that matches those colors. How can I do this?

    
asked by anonymous 01.11.2015 / 17:59

2 answers

0

Basis of how you want to adapt Leandro's code:

    $("#azul").click(function() {
    $("body").css({
    "background": "url(azul.png)";
    });

    $("#cinza").click(function() {
    $("body").css({
    "background": "url(cinza.png)";
    });

    $("#vermelho").click(function() {
    $("body").css({
    "background": "url(vermelho.png)";
    });
    });
    
01.11.2015 / 22:37
0

Good with javascript is more difficult ... I will use jquery if you want to use usa!

<div id="azul">azul</div>
<br>
<div id="vermelho">vermelho</div>
<br>
<div id="cinza">cinza</div>
<br>

Well, just read and code is not difficult ... anything is false

$("#azul").click(function() {
      $("body").css({
        "background": "blue";
     });

      $("#cinza").click(function() {
        $("body").css({
          "background": "#ccc";
        });

        $("#vermelho").click(function() {
          $("body").css({
            "background": "red";
          });
        });

EDIT: Leandro, do not forget to put ";" to close. This is important.

    
01.11.2015 / 19:04