/    Sign up×
Community /Pin to ProfileBookmark

Multiple Color change onclick using color wheel

I am trying to get this javascript to change the body bgcolor onclick with a color picker on the first button and maincontent bgcolor on the second button. I can get both buttons to change one or the other, but not separately. Any help would be greatly appreciated.

[url]www.element57.homeip.net[/url]

(function() {
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;

function createCP(idcount)
{
YAHOO.util.Event.onContentReady(“button-container”+idcount, function () {
function onButtonOption() {

/*
Create a new ColorPicker instance, placing it inside the body
element of the Menu instance.
*/
var oColorPicker = new YAHOO.widget.ColorPicker(oColorPickerMenu.body.id, {
showcontrols: false,
images: {
PICKER_THUMB: “http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/picker_thumb.png”,
HUE_THUMB: “http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/hue_thumb.png”
}
});
// Align the Menu to its Button
oColorPickerMenu.align();
/*
Add a listener for the ColorPicker instance’s “rgbChange” event
to update the background color and text of the Button’s
label to reflect the change in the value of the ColorPicker.
*/
oColorPicker.on(“rgbChange”, function (p_oEvent) {
var sColor = “#” + this.get(“hex”);
oButton.set(“value”, sColor);
//UPDATED CODE HERE
YAHOO.util.Dom.setStyle(“current-color-” + idcount, “backgroundColor”, sColor);
YAHOO.util.Dom.get(“current-color-” + idcount).innerHTML = “Current color is ” + sColor;
YAHOO.util.Dom.get(“hex”+idcount).value = sColor;
});
// Remove this event listener so that this code runs only once
this.unsubscribe(“option”, onButtonOption);
}
// Create a Menu instance to house the ColorPicker instance
var oColorPickerMenu = new YAHOO.widget.Menu(“color-picker-menu”+idcount);
// Create a Button instance of type “split”
var oButton = new YAHOO.widget.Button({
type: “split”,
id: “color-picker-button”+idcount,
//UPDATED CODE HERE
label: “<em id=”current-color-” + idcount + “”>Current color is #FFFFFF.</em>”,
menu: oColorPickerMenu,
container: this });
oButton.on(“appendTo”, function () {

/*
Create an empty body element for the Menu instance in order to
reserve space to render the ColorPicker instance into.
*/

oColorPickerMenu.setBody(” “);
oColorPickerMenu.body.id = “color-picker-container”+idcount;

// Render the Menu into the Button instance’s parent element

oColorPickerMenu.render(this.get(“container”));

});

/*
Add a listener for the “option” event. This listener will be
used to defer the creation the ColorPicker instance until the
first time the Button’s Menu instance is requested to be displayed
by the user.
*/
oButton.on(“option”, onButtonOption);
oButton.on(“click”, function () {

YAHOO.util.Dom.setStyle(“photo”, “backgroundColor”, this.get(“value”));

});

});

}

createCP(1); createCP(2); createCP(3);

})();

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@bassnrageDec 07.2008 — hi, there -

i'm thinking your problem must be here:

<i>
</i>YAHOO.util.Dom.setStyle("photo", "backgroundColor", this.get("value"));


seems to me this is the only place where you are setting a color. this function seems to be setting the style of the "backgroundColor" for the element with id "photo" to be the "value" of the input field. so you need to set it to be "photo" for one of them and "body" for the other.

so maybe you need to do something like this:

<i>
</i>function createCP(idcount,styleId)


and then...
<i>
</i>YAHOO.util.Dom.setStyle(styleId, "backgroundColor", this.get("value"));


and then...
<i>
</i>createCP(1,"photo"); createCP(2,"body");


i think...

hope this helps.

mike

Javascript Form Validator FREE!
Copy linkTweet thisAlerts:
@jaiantbassauthorDec 07.2008 — Thanks for the reply Mike. I see what you are saying. But where does

<i>
</i>function createCP(idcount,styleId)


this fit in? I am still relatively new to javascript. I really appreciate your help.

Andrew
Copy linkTweet thisAlerts:
@jaiantbassauthorDec 07.2008 — Sorry Mike, I just got it. Works great! I did just what you suggested -

<i>
</i>
(function() {
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;

function createCP(idcount,styleId)
{
YAHOO.util.Event.onContentReady("button-container"+idcount, function () {
function onButtonOption() {

<i> </i> /*
<i> </i> Create a new ColorPicker instance, placing it inside the body
<i> </i> element of the Menu instance.
<i> </i> */
<i> </i> var oColorPicker = new YAHOO.widget.ColorPicker(oColorPickerMenu.body.id, {
<i> </i> showcontrols: false,
<i> </i> images: {
<i> </i> PICKER_THUMB: "http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/picker_thumb.png",
<i> </i> HUE_THUMB: "http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/hue_thumb.png"
<i> </i> }
<i> </i> });
<i> </i> // Align the Menu to its Button
<i> </i> oColorPickerMenu.align();
<i> </i> /*
<i> </i> Add a listener for the ColorPicker instance's "rgbChange" event
<i> </i> to update the background color and text of the Button's
<i> </i> label to reflect the change in the value of the ColorPicker.
<i> </i> */
<i> </i> oColorPicker.on("rgbChange", function (p_oEvent) {
<i> </i> var sColor = "#" + this.get("hex");
<i> </i> oButton.set("value", sColor);
<i> </i> //UPDATED CODE HERE
<i> </i> YAHOO.util.Dom.setStyle("current-color-" + idcount, "backgroundColor", sColor);
<i> </i> YAHOO.util.Dom.get("current-color-" + idcount).innerHTML = "Current color is " + sColor;
<i> </i> YAHOO.util.Dom.get("hex"+idcount).value = sColor;
<i> </i> });
<i> </i> // Remove this event listener so that this code runs only once
<i> </i> this.unsubscribe("option", onButtonOption);
<i> </i> }
<i> </i> // Create a Menu instance to house the ColorPicker instance
<i> </i> var oColorPickerMenu = new YAHOO.widget.Menu("color-picker-menu"+idcount);
<i> </i> // Create a Button instance of type "split"
<i> </i> var oButton = new YAHOO.widget.Button({
<i> </i> type: "split",
<i> </i> id: "color-picker-button"+idcount,
<i> </i> //UPDATED CODE HERE
<i> </i> label: "&lt;em id="current-color-" + idcount + ""&gt;Current color is #FFFFFF.&lt;/em&gt;",
<i> </i> menu: oColorPickerMenu,
<i> </i> container: this });
<i> </i> oButton.on("appendTo", function () {

<i> </i> /*
<i> </i> Create an empty body element for the Menu instance in order to
<i> </i> reserve space to render the ColorPicker instance into.
<i> </i> */

<i> </i> oColorPickerMenu.setBody(" ");
<i> </i> oColorPickerMenu.body.id = "color-picker-container"+idcount;



<i> </i> // Render the Menu into the Button instance's parent element

<i> </i> oColorPickerMenu.render(this.get("container"));

<i> </i> });


<i> </i> /*
<i> </i> Add a listener for the "option" event. This listener will be
<i> </i> used to defer the creation the ColorPicker instance until the
<i> </i> first time the Button's Menu instance is requested to be displayed
<i> </i> by the user.
<i> </i> */




<i> </i> oButton.on("option", onButtonOption);
<i> </i> oButton.on("click", function () {


<i> </i> YAHOO.util.Dom.setStyle(styleId, "backgroundColor", this.get("value"));


<i> </i> });



<i> </i>});

}


<i> </i>createCP(1,"photo"); createCP(2,"body");


})();




Thanks again - this one was starting to get to me, being new and all to javascript.

Andrew
Copy linkTweet thisAlerts:
@bassnrageDec 08.2008 — glad it worked! ?

mike

Javascript Form Validation Tool FREE!
Copy linkTweet thisAlerts:
@jaiantbassauthorDec 08.2008 — Sorry to drag this out, but now I can't seem to get the color picker to come in IE. It works in Safari and FF.

www.element57.homeip.net

Here is all the code:

HTML
<i>
</i>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;Color Picker Button&lt;/title&gt;

&lt;link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/slider/assets/skins/sam/slider.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/fonts/fonts-min.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/colorpicker/assets/skins/sam/colorpicker.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/menu/assets/skins/sam/menu.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/button/assets/skins/sam/button.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="plain.css" /&gt;
&lt;script type="text/javascript" src="core.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/dragdrop/dragdrop-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/slider/slider-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/colorpicker/colorpicker-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/container/container_core-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/menu/menu-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/button/button-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="color.js"&gt;&lt;/script&gt;

&lt;!-- rounded corners .js --&gt;


&lt;script type="text/JavaScript" src="curvycorners.js"&gt;&lt;/script&gt; <br/>
&lt;script type="text/JavaScript" src="roundy.js"&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body class=" yui-skin-sam" id="body"&gt;


&lt;h1&gt;Pick Your Colors&lt;/h1&gt;


&lt;div id="photo"&gt;&lt;/div&gt;



&lt;div id="button-container1"&gt;&lt;label for="color-picker-button1"&gt;MainContent: &lt;/label&gt;&lt;/div&gt;
&lt;input type="text" name="hex1" id="hex1"&gt;
&lt;div id="button-container2"&gt;&lt;label for="color-picker-button2"&gt;Background: &lt;/label&gt;&lt;/div&gt;
&lt;input type="text" name="hex2" id="hex2"&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;input type="button" value="Reload Page" onClick="window.location.reload()"&gt;


&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/utilities/utilities.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/slider/slider.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/colorpicker/colorpicker-beta.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/container/container_core.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/menu/menu.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/button/button.js"&gt;&lt;/script&gt;

&lt;script src="http://us.js2.yimg.com/us.js.yimg.com/i/ydn/yuiweb/js/dpsyntax-min-2.js"&gt;&lt;/script&gt;



&lt;/body&gt;
&lt;/html&gt;





CSS
<i>
</i>/*margin and padding on body element
can introduce errors in determining
element position and are not recommended;
we turn them off as a foundation for YUI
CSS treatments. */


<i> </i>p, h2 {
<i> </i> margin: 1em;
<i> </i> }
<i> </i>/*
<i> </i> Set the "zoom" property to "normal" since it is set to "1" by the
<i> </i> ".example-container .bd" rule in yui.css and this causes a Menu
<i> </i> instance's width to expand to 100% of the browser viewport.
<i> </i>*/

<i> </i>div.yuimenu .bd {

<i> </i> zoom: normal;

<i> </i>}

<i> </i>#button-container {

<i> </i> padding: .5em;

<i> </i>}

<i> </i>#color-picker-button button {

<i> </i> outline: none; /* Safari */
<i> </i> line-height: 1.5;

<i> </i>}


<i> </i>/*
<i> </i> Style the Button instance's label as a square whose background color
<i> </i> represents the current value of the ColorPicker instance.
<i> </i>*/

<i> </i>#current-color-1,
<i> </i>#current-color-2 ,
<i> </i>#current-color-3 {

<i> </i> display: block;
<i> </i> display: inline-block;
<i> </i> *display: block; /* For IE */
<i> </i> margin-top: .5em;
<i> </i> *margin: .25em 0; /* For IE */
<i> </i> width: 1em;
<i> </i> height: 1em;
<i> </i> overflow: hidden;
<i> </i> text-indent: 1em;
<i> </i> background-color: #fff;
<i> </i> white-space: nowrap;
<i> </i> border: solid 1px #000;

<i> </i>}


<i> </i>/* Hide default colors for the ColorPicker instance. */

<i> </i>#color-picker-container .yui-picker-controls,
<i> </i>#color-picker-container .yui-picker-swatch,
<i> </i>#color-picker-container .yui-picker-websafe-swatch {

<i> </i> display: none;

<i> </i>}


<i> </i>/*
<i> </i> Size the body element of the Menu instance to match the dimensions of
<i> </i> the ColorPicker instance.
<i> </i>*/

<i> </i>#color-picker-menu .bd {

<i> </i> width: 220px;
<i> </i> height: 190px;

<i> </i>}

#photo {



<i> </i>border: solid 1px #000;
<i> </i>width: 600px;
<i> </i>height: 300px;
<i> </i>margin: 0 auto;
<i> </i>_width: 398px; /* For IE 6 */
<i> </i>_height: 298px; /* For IE 6 */

}


#button-container {

<i> </i>border-top: solid 1px #000;
<i> </i>padding: .5em .25em;
<i> </i>margin-top: .5em;

}




Javascript

<i>
</i>
/*margin and padding on body element
can introduce errors in determining
element position and are not recommended;
we turn them off as a foundation for YUI
CSS treatments. */


<i> </i>p, h2 {
<i> </i> margin: 1em;
<i> </i> }
<i> </i>/*
<i> </i> Set the "zoom" property to "normal" since it is set to "1" by the
<i> </i> ".example-container .bd" rule in yui.css and this causes a Menu
<i> </i> instance's width to expand to 100% of the browser viewport.
<i> </i>*/

<i> </i>div.yuimenu .bd {

<i> </i> zoom: normal;

<i> </i>}

<i> </i>#button-container {

<i> </i> padding: .5em;

<i> </i>}

<i> </i>#color-picker-button button {

<i> </i> outline: none; /* Safari */
<i> </i> line-height: 1.5;

<i> </i>}


<i> </i>/*
<i> </i> Style the Button instance's label as a square whose background color
<i> </i> represents the current value of the ColorPicker instance.
<i> </i>*/

<i> </i>#current-color-1,
<i> </i>#current-color-2 ,
<i> </i>#current-color-3 {

<i> </i> display: block;
<i> </i> display: inline-block;
<i> </i> *display: block; /* For IE */
<i> </i> margin-top: .5em;
<i> </i> *margin: .25em 0; /* For IE */
<i> </i> width: 1em;
<i> </i> height: 1em;
<i> </i> overflow: hidden;
<i> </i> text-indent: 1em;
<i> </i> background-color: #fff;
<i> </i> white-space: nowrap;
<i> </i> border: solid 1px #000;

<i> </i>}


<i> </i>/* Hide default colors for the ColorPicker instance. */

<i> </i>#color-picker-container .yui-picker-controls,
<i> </i>#color-picker-container .yui-picker-swatch,
<i> </i>#color-picker-container .yui-picker-websafe-swatch {

<i> </i> display: none;

<i> </i>}


<i> </i>/*
<i> </i> Size the body element of the Menu instance to match the dimensions of
<i> </i> the ColorPicker instance.
<i> </i>*/

<i> </i>#color-picker-menu .bd {

<i> </i> width: 220px;
<i> </i> height: 190px;

<i> </i>}

#photo {



<i> </i>border: solid 1px #000;
<i> </i>width: 600px;
<i> </i>height: 300px;
<i> </i>margin: 0 auto;
<i> </i>_width: 398px; /* For IE 6 */
<i> </i>_height: 298px; /* For IE 6 */

}


#button-container {

<i> </i>border-top: solid 1px #000;
<i> </i>padding: .5em .25em;
<i> </i>margin-top: .5em;

}



Thanks again!
×

Success!

Help @jaiantbass spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...