Search code examples
javascriptjqueryhtmlpaintcolor-picker

jquery - Fill div with color from color-picker


I am making a simple painting program in jQuery and HTML. For this I am using the Spectrum color picker (http://bgrins.github.io/spectrum/). I have made a grid of hundreds of 16x16 squares (div class pixel), and when one of them is clicked, it will get filled in with the color from the Spectrum color picker, but I don't know how to. Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css.css" />
<title>Pixel Painter</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><!-- Jquery -->
<script src='jscolor/jscolor.js'></script> <!--Spectrum js-->
<link rel='stylesheet' href='jscolor/jscolor.css' /> <!--Spectrum css-->
</head>
<body>
<input id='colorpicker' />
<script>
$("#colorpicker").spectrum({ //spectrum config
    color: "#f00",
    showPalette: true,
    showInput: true,
    showAlpha: true,
    chooseText: "Pick",
    cancelText: "X",
    togglePaletteOnly: true,
    showPaletteOnly: true,
    togglePaletteMoreText: "More Colors...",
    togglePaletteLessText: "Less Colors...",
        palette: [
            //color palettes for Spectrum
        ]
});
</script>
<br/>
<table border="1" style="background-color:#FFFFFF; border-collapse:collapse;border:1px solid #000000;color:#000000;width:100%" cellpadding="3" cellspacing="0">
    <tr>
        <!-- Grid -->
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
    </tr>
    <tr>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
        <div class="pixel"></div>
    </tr>
<!--... and a lot more <div class="pixel"></div>-->
</table>
</body>
</html>

http://jsfiddle.net/jGb33/


Solution

  • You can do like this :

    var color = "#f00";
    
    $("#colorpicker").spectrum({ //spectrum config
      color: "#f00",
      showPalette: true,
      showInput: true,
      showAlpha: true,
      chooseText: "Pick",
      cancelText: "X",
      togglePaletteOnly: true,
      showPaletteOnly: true,
      togglePaletteMoreText: "More Colors...",
      togglePaletteLessText: "Less Colors...",
      palette: [
            //color palettes for Spectrum
      ],
      change: function(colorSelected) {
        color = colorSelected.toHexString();
      }
    });
    
    $('.pixel').click(function(){
    
      $(this).css('background-color' , color);
    
    });
    

    http://jsfiddle.net/jGb33/1/