Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery - help to create the simplest color picker ever
    primarykey
    data
    text
    <p>Stackoverflow is awesome! I just posted a question and it was answered by the community within minutes and a working answer was obtained!</p> <p>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.</p> <p>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'.</p> <pre><code>&lt;div class="supercolor" &gt; &lt;div class="colordiv" id="#111111" style="background-color:#111111;"&gt;&amp;nbsp;&lt;/div&gt; &lt;div class="colordiv" id="#222222" style="background-color:#222222;" &gt;&amp;nbsp;&lt;/div&gt; &lt;div class="colordiv" id="#333333" style="background-color:#333333;"&gt;&amp;nbsp;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>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.</p> <p>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.</p> <pre><code>var selected_arry = []; $('#111111').click(function(){ if (selected_arry == 1){ selected_arry[0] = 0; $('#111111').html(""); } else { selected_arry = 1; $('#111111').html("&amp;#10003;&lt;input type='checkbox' name='selected_color[]' hidden checked id='#111111' /&gt;"); } }); -&gt;repeat same code for next 15 divs but with different ID </code></pre> <p>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.</p> <p>Thank you very much!</p> <p><strong>Edit : Working Code</strong></p> <p>Finally with the help from @egis &amp; @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.</p> <p>CSS Code:</p> <pre><code>&lt;style type="text/css"&gt; .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;} &lt;/style&gt; </code></pre> <p>HTML Code:</p> <pre><code>&lt;div id="colour-picker"&gt; &lt;div class="colour red"&gt;&amp;nbsp;&lt;/div&gt; &lt;div class="colour green"&gt;&amp;nbsp;&lt;/div&gt; &lt;div class="colour blue"&gt;&amp;nbsp;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Script Code:</p> <pre><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("&amp;#10003;&lt;input type='checkbox' name='selected_color[]' hidden checked id='"+colour+"' value='"+colour+"' /&gt;"); } else { $this.html(''); }; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload