Frontend Development

Creating a color picker using jQuery

Submitted by Burned Designs, , Thread ID: 20922

Thread Closed
Burned Designs
Web Developer/Designer
Prime
Level:
0
Reputation:
5
Posts:
115
Likes:
9
Credits:
484
05-05-2016, 04:31 AM
#1
This will be a simple tutorial for using cookies and a color changer.

So before we get started on any jquery we have to set up the html for the simple color selector. (once advanced enough you can use this color picker Link


Code:
<span id="showBox">Show</span>
<div id="colorBox">
  <div class="red"></div>
  <div class="blue"></div>
  <div class="orange"></div>
  <div class="purple"></div>
</div>

That's it for the html portion of the color picker.

Now lets style that html so it looks pertty.


Code:
span#showBox{
background:#fff;
border:1px solid #ccc;
padding:5px 10px;
border-radius:4px;
cursor:pointer;
font-family:arial,sans-serif;
color:#333;
}

div#colorBox{
margin-top:10px;
background:#fff;
border:1px solid #ccc;
padding:10px;
width:65px;
display:none;
}

div#colorBox > div{
height:30px;
width:30px;
display:inline-block;
}

.red{
background:#c13333;
}

.blue{
background:#336184;
}

.orange{
background:#fd8a17;
}

.purple{
background:#4c3382;
}

Now that our color box looks more pertty than your own face lets start on the jquery because if you just copy and past the code above you wont see anything aside from the show button. This color changer does work with cookies so it saves per browser.

Code:
$(document).ready(function(){
$("#showBox").click(function(){
  $("#colorBox").slideToggle();
});

  $(".red").click(function(){
    $('#container').addClass('red');
    $('#container').removeClass('blue');
    $('#container').removeClass('orange');
    $('#container').removeClass('purple');
    $.cookie("mycookie","red",{expires:999});
  });
  
   $(".blue").click(function(){
    $('#container').addClass('blue');
    $('#container').removeClass('red');
    $('#container').removeClass('orange');
    $('#container').removeClass('purple');
    $.cookie("mycookie","blue",{expires:999});
  });

$(".orange").click(function(){
    $('#container').addClass('orange');
    $('#container').removeClass('red');
    $('#container').removeClass('blue');
    $('#container').removeClass('purple');
    $.cookie("mycookie","orange",{expires:999});
  });
  
  $(".purple").click(function(){
    $('#container').addClass('purple');
    $('#container').removeClass('red');
    $('#container').removeClass('orange');
    $('#container').removeClass('blue');
    $.cookie("mycookie","purple",{expires:999});
  });


  //Check if cookie exists
  if (typeof $.cookie('mycookie') === "undefined"){
  //no cookie
  } else {
  //cookie is there, do something
   $('#container').addClass($.cookie('mycookie'));
  }
});
[Image: SIG.PNG]

RE: Creating a color picker using jQuery

Zenith
we will wait for this
Prime
Level:
2
Reputation:
340
Posts:
4.81K
Likes:
1.06K
Credits:
2.5K
05-05-2016, 04:50 AM
#2
Thanks dude. I will be trying this with my site in a bit. I'm shit at jQuery and I've wanted to do this for a while

I will update

[Image: Yp8ZHSk.gif]

RE: Creating a color picker using jQuery

Burned Designs
Web Developer/Designer
Prime
Level:
0
Reputation:
5
Posts:
115
Likes:
9
Credits:
484
OP
05-05-2016, 05:07 AM
This post was last modified: 05-05-2016, 05:11 AM by Burned Designs
#3
You can expand past adding classes with this jquery you can have an if statement like this
Code:
if($("#container").hasClass('red')){
  $("#container").css({"border":"1px solid #c13333","background":"#efefef"});
}

Be warned though If you are using ajax on a set of code that color will not take affect unless you load that cookie before the ajax is loaded which can be done in css and jquery like so
Code:
/* CSS Below */
html,body{
  visiblity:hidden;
}

// Jquery below
document.getElementsByTagName("html")[0].style.visibility = "visible";
document.getElementsByTagName("body")[0].style.visibility = "visible";
[Image: SIG.PNG]

RE: Creating a color picker using jQuery

Maskovar
Novice
Level:
0
Reputation:
0
Posts:
23
Likes:
1
Credits:
25
22-05-2016, 03:31 AM
#4
Thanks for the basic yet useful share.

RE: Creating a color picker using jQuery

Junior Member
Level:
0
Reputation:
0
Posts:
74
Likes:
6
Credits:
0
22-05-2016, 04:50 AM
#5
hopefully I can try to make a cool userbar color customizer for this :-0

RE: Creating a color picker using jQuery

Burned Designs
Web Developer/Designer
Prime
Level:
0
Reputation:
5
Posts:
115
Likes:
9
Credits:
484
OP
22-05-2016, 10:24 AM
#6
Ive actually set up the advanced color picker using the eyecon color picker posted at the beginning if you guys want a tutorial on that let me know below and ill post it.
[Image: SIG.PNG]

RE: Creating a color picker using jQuery

extrememetal
Newbie
Level:
0
Reputation:
0
Posts:
19
Likes:
0
Credits:
8
07-12-2016, 10:41 AM
#7
color picker with jquery does the thing, thanks...

RE: Creating a color picker using jQuery

raspucay
Newbie
Level:
0
Reputation:
0
Posts:
16
Likes:
0
Credits:
1
15-03-2017, 03:30 PM
#8
What do i need to set this up? VPS or does just shared hosting work?

RE: Creating a color picker using jQuery

kahraman451
Newbie
Level:
0
Reputation:
0
Posts:
18
Likes:
0
Credits:
4
25-10-2017, 12:16 AM
#9
05-05-2016, 04:31 AM
BurnedDesigns Wrote:
This will be a simple tutorial for using cookies and a color changer.

So before we get started on any jquery we have to set up the html for the simple color selector. (once advanced enough you can use this color picker Link


Code:
<span id="showBox">Show</span>
<div id="colorBox">
  <div class="red"></div>
  <div class="blue"></div>
  <div class="orange"></div>
  <div class="purple"></div>
</div>

That's it for the html portion of the color picker.

Now lets style that html so it looks pertty.


Code:
span#showBox{
background:#fff;
border:1px solid #ccc;
padding:5px 10px;
border-radius:4px;
cursor:pointer;
font-family:arial,sans-serif;
color:#333;
}

div#colorBox{
margin-top:10px;
background:#fff;
border:1px solid #ccc;
padding:10px;
width:65px;
display:none;
}

div#colorBox > div{
height:30px;
width:30px;
display:inline-block;
}

.red{
background:#c13333;
}

.blue{
background:#336184;
}

.orange{
background:#fd8a17;
}

.purple{
background:#4c3382;
}

Now that our color box looks more pertty than your own face lets start on the jquery because if you just copy and past the code above you wont see anything aside from the show button. This color changer does work with cookies so it saves per browser.

Code:
$(document).ready(function(){
$("#showBox").click(function(){
  $("#colorBox").slideToggle();
});

  $(".red").click(function(){
    $('#container').addClass('red');
    $('#container').removeClass('blue');
    $('#container').removeClass('orange');
    $('#container').removeClass('purple');
    $.cookie("mycookie","red",{expires:999});
  });
  
   $(".blue").click(function(){
    $('#container').addClass('blue');
    $('#container').removeClass('red');
    $('#container').removeClass('orange');
    $('#container').removeClass('purple');
    $.cookie("mycookie","blue",{expires:999});
  });

$(".orange").click(function(){
    $('#container').addClass('orange');
    $('#container').removeClass('red');
    $('#container').removeClass('blue');
    $('#container').removeClass('purple');
    $.cookie("mycookie","orange",{expires:999});
  });
  
  $(".purple").click(function(){
    $('#container').addClass('purple');
    $('#container').removeClass('red');
    $('#container').removeClass('orange');
    $('#container').removeClass('blue');
    $.cookie("mycookie","purple",{expires:999});
  });


  //Check if cookie exists
  if (typeof $.cookie('mycookie') === "undefined"){
  //no cookie
  } else {
  //cookie is there, do something
   $('#container').addClass($.cookie('mycookie'));
  }
});

very nice Smile

Users browsing this thread: 1 Guest(s)