Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Using toggle()

why do you need "+"

when assigning the valu of the rgb whith the value of the var "r" "g" and "b", why do you need to write rgb("+r+","+g+","+b+") and not just ("r","g","b")?

//Problem: No user interaction causes no change to application
//Solution: When user interacts cause changes appropriately
var color = $(".selected").css("background-color");

//When clicking on control list items
$(".controls li").click(function(){
  //Deselect sibling elements
  //Select clicked element
  //cache current color
  color = $(this).css("background-color");

//When new color is pressed
  //Show color select or hide the color select

//update the new color span
function changeColor() {
  var r = $("#red").val();
  var g = $("#green").val();
  var b = $("#blue").val();
  $("#newColor").css("background-color", "rgb(" + r + "," + g +", " + b + ")");

//When color sliders change

//When add color is pressed
  //Append the color to the controls ul
  //Select the new color

//On mouse events on the canvas
  //Draw lines
<!DOCTYPE html>
    <title>Simple Drawing Application</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
    <canvas width="600" height="400"></canvas>
    <div class="controls">
            <li class="red selected"></li>
            <li class="blue"></li>
            <li class="yellow"></li>
        <button id="revealColorSelect">New Color</button>
        <div id="colorSelect">
            <span id="newColor"></span>
            <div class="sliders">
                    <label for="red">Red</label>
                    <input id="red" name="red" type="range" min=0 max=255 value=0>
                    <label for="green">Green</label>
                    <input id="green" name="green" type="range" min=0 max=255 value=0>
                    <label for="blue">Blue</label>
                    <input id="blue" name="blue" type="range" min=0 max=255 value=0>
            <button id="addNewColor">Add Color</button>
    <script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>    

1 Answer

Samuel Ferree
Samuel Ferree
31,721 Points

the jQuery function .css() expects two parameters, both of which should be strings.

you need to convert your numerical r, g and b values, into a string of text, that meets the requirements of the rgb() css property.

The '+' operator, concatenates strings. consider the following code

let greeting = 'hello' + ' ' + 'world'; //=> 'hello world'
let r = 100;
let g = 200;
let b = 255;
let text = 'rgb(' + r + ',' + g + ',' + b + ');'; //=> 'rgb(100,200,255)'
Samuel Ferree
Samuel Ferree
31,721 Points

If you prefer something a bit more readable, ES5 gave us template literals. which can interpolate values by placing them inside of ${}

consider the following code:

let r = 100;
let g = 200;
let b = 255;
let text = `rgb(${r},${g},${b})`; // => 'rgb(100,200,255)'

Oh I see. I didn'r realize that it took a string. thanx