Stackoverflow is awesome! I just posted a question and it was answered by the community within minutes and a working answer was obtained!

I have another question about a custom color picker. I know there are many color picker out there but it is just too complicated to use. I need a simpler one.

So I decided to create a simple 4x4 pallet of predefined color. The pallets are formed using div with different background color under a div-class named 'supercolor'.

```
<div class="supercolor" >                       
  <div class="colordiv" id="#111111" style="background-color:#111111;">&nbsp;</div>
  <div class="colordiv" id="#222222" style="background-color:#222222;" >&nbsp;</div>                
  <div class="colordiv" id="#333333" style="background-color:#333333;">&nbsp;</div>
</div>

```

In my script section, I manually add on click function to each and individual id to dynamically create an (input option check box checked html) and a tick character on the division so the user knows that color is selected. If the division is already checked, it will removed the (input option check box checked html) and the tick character.

I used an array to determined if the div is checked, and if it is, update that array index. The input check box are created so when the page submits, I have a way to know which color was selected based on the check box value which is the background color in hex.

```
var selected_arry = [];

$('#111111').click(function(){
  if (selected_arry == 1){
    selected_arry[0] = 0;
    $('#111111').html("");
  } else {
    selected_arry = 1;
    $('#111111').html("&#10003;<input type='checkbox' name='selected_color[]' hidden checked id='#111111' />");
  }
});

->repeat same code for next 15 divs but with different ID

```

My question is, I have to repeat this for all the division I create and it does look very unoptimized and I think there must be a better way to do it. I only have a few months of Jquery exposure and a newbie developer. I am hoping all the gurus out there can point me to a more efficient way.

Thank you very much!

**Edit : Working Code**

Finally with the help from @egis & @Rob Cowie, the code is completed with very efficient and scalable function. Note: I removed some part (making it simpler for beginner like me to understand) and edited some part to allow multiple selection.

CSS Code:

```
<style type="text/css">
   .colour {
   width: 40px;
   height: 40px;
   float: left;
   cursor: pointer;
   line-height: 40px;
   text-align: center;
   }
   .red {background-color: #FF2622;}
   .green {background-color: #2B6DB9;}
   .blue {background-color: #4AFF41;}
</style>

```

HTML Code:

```
<div id="colour-picker">
   <div class="colour red">&nbsp;</div>
   <div class="colour green">&nbsp;</div>
   <div class="colour blue">&nbsp;</div>
</div>

```

Script Code:

```
$(document).ready(function() {
   $('.colour').click(function(event){
      var $this = $(this);
   $this.toggleClass('selected');
       if ($this.hasClass('selected')){
          var colour = $this.css('backgroundColor');
      $this.html("&#10003;<input type='checkbox' name='selected_color[]' hidden checked id='"+colour+"' value='"+colour+"' />");
       } else {
          $this.html('');
       };
   });
});

```