Click here to Skip to main content
Licence CPOL
First Posted 14 Sep 2007
Views 17,370
Downloads 50
Bookmarked 17 times

A Color Picker Control

By | 14 Sep 2007 | Article
An client-side color picker control using JavaScript.

Screenshot - ColorPiker1.jpg

Introduction

I was working on an ASP.NET project and came across the requirement for a color picker. I searched on the net for one and I came across a dropdwonlist selector and popup control. But it was not what I was looking for, so I created one which suited my requirements. Well, it's a simple one, and does solve my problems to an extent.

Using the code

We are using an HTML table where each cell has a different color. On clicking a link, this table will hide/show. When the user click on a cell, a JavaScript function is called, which assigns the color code to the textbox. Each table cell has its background color attribute set. On clicking a cell, we calling the JavaScript function onclick="javascript:toggleLayer( 'color1' , this,'TextBox1');", which will set the selected cell's background color to that of the textbox.

Here is the JavaScript:

//
function toggleLayer( whichLayer, Colorcntrl ,txtboxcntrl)
{
  var elem, vis;
      if( document.getElementById ) 
        elem = document.getElementById( whichLayer );
            else if( document.all )
                    elem = document.all[whichLayer];
                        else if( document.layers )
                            elem = document.layers[whichLayer];
  vis = elem.style;
  
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
      vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  
  //Set Textbox value and background
  if(Colorcntrl != null && txtboxcntrl != null)
  {
    document.getElementById(txtboxcntrl).value = Colorcntrl.style.backgroundColor;
    document.getElementById(txtboxcntrl).style.backgroundColor = 
                                         Colorcntrl.style.backgroundColor;
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dyno's

Team Leader

India India

Member

Hi, m developer.Presently working on .NET Technology.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI think you mean Picker! PinmemberSpeednet_6:24 15 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 14 Sep 2007
Article Copyright 2007 by Dyno's
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid