Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 5

alahab2
alahab2
9,709 Points

Variables with and without $

This a question not to this particular video, but to the tutorial in general.

Here is the code we've generated so far

//Problem: No user interaction causes no change to application
//Solution: When user interacts cause changes appropriately
var color = $(".selected").css("background-color");
var context = $("canvas")[0].getContext("2d");

//When clicking on control li

$(".controls").on("click", "li", function() {
//Deselect siblings
$(this).siblings().removeClass("selected");
//Select clicked
$(this).addClass("selected");

color = $(this).css("background-color");
}); 


//When new color is clicked
$("#revealColorSelect").click(function() {
//show or hide the control panel
changeColor();
//show or hide the control panel 
$("#colorSelect").toggle();

});


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
$("input[type=range]").on("input", changeColor);
  //update new color span

//When add color button pressed 
$("#addNewColor").click(function() {
  //append the color to the controls ul
var $newColor = $("<li></li>");
$newColor.css("background-color", $("#newColor").css("background-color"));
$(".controls ul").append($newColor);
//select the new color
$newColor.click();
});

  //On mouse events on the canvas 
var $canvas = $("canvas");  
  //draw lines
context.beginPath();
context.moveTo(10,10);
context.lineTo(80,10);
context.stroke();

Why some of the variables we created have $ in the beginning and some (like r,g and b) don't? Would there be a difference in the code execution if i put $r, $g and $b?

Thanks in advance!

1 Answer

Andrew Kiernan
Andrew Kiernan
26,892 Points

Hello!

In the case of r, b, and g versus $newColor and $canvas, the dollar sign has no bearing on the code execution, the $ is just a stylistic choice to denote that those variables hold jQuery objects. In general practice, you can use $ to start a variable name whenever you like.

The $ followed by the parenthesis (e.g. $('canvas')) is a jQuery selector which returns a collection of all HTML elements that match the selector, which in my example would be all canvas elements.

Hope that helps! Let me know if you have any questions.

-Andrew

alahab2
alahab2
9,709 Points

Thanks a lot, that really helps!