Help with Multiple Color Picker program

Discussion in 'JavaScript' started by jaiantbass, Dec 7, 2008.

  1. #1
    I am trying to get two buttons to change different backgrounds and can only get them to change the same thing. Here is the web site and code. Button one should change the body's bgcolor and button two should change the maincontent bgcolor. Thanks,

    www.element57.homeip.net

    (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);


    })();
     
    jaiantbass, Dec 7, 2008 IP
  2. jaiantbass

    jaiantbass Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    With some help, I got this one squared away. Here is the solution:

    
    
    (function() {
        var Dom = YAHOO.util.Dom,
            Event = YAHOO.util.Event;
    
    function createCP(idcount,styleId)
    {
        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(styleId, "backgroundColor", this.get("value"));
    			
            
            });
    
         
    		
        });
    
    }
    
    
    	createCP(1,"photo"); createCP(2,"body");
    
    
    })();
    
    
    
    Code (markup):
    had to add these lines:

    
    
    function createCP(idcount,styleId)
    
    
    Code (markup):

    
    
    YAHOO.util.Dom.setStyle(styleId, "backgroundColor", this.get("value"));
    
    
    Code (markup):

    
    createCP(1,"photo"); createCP(2,"body");
    
    
    Code (markup):
    Works perfectly.
     
    jaiantbass, Dec 7, 2008 IP