Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4603340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:07:58+00:00 2026-05-22T00:07:58+00:00

Stackoverflow is awesome! I just posted a question and it was answered by the

  • 0

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 4×4 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('');
       };
   });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:07:58+00:00Added an answer on May 22, 2026 at 12:07 am

    I’ve created a solution that delivers what I think you’re after. See https://gist.github.com/962872 for the code.

    In summary, clicking on colour divs toggles the class ‘selected’ and sets the value of a hidden form input (which will submit the colour).

    If the clicked div is currently selected, the class is removed, and the value of the hidden input is set to empty.

    You can do more on click simply by adding to the click handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to the gurus at StackOverflow. You guys are awesome. I posted on question
After stackoverflow answered my previous question on here about my Wiimote left/right click issue,
Hello StackOverflow community, Using Google App Engine, I wrote a keyToSha256() method within a
Hi StackOverflow community, in my application I'd like to have a button which allows
Hello stackoverflow community, I have some code that works on virtually every browser except
this is my first time posting on StackOverflow but this site is awesome. Thanks
This StackOverflow answer has an image of KDiff3 highlighting intra-line differences. Does someone know
Hello stackoverflow community ! I am trying to figure out how to architect my
Hello StackOverflow'ers, I have a (flex) app that, on the click of a button,
Hi stackoverflow coders, I have an app (since the iPhone its really chic to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.