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 trialRiccardo Busin
11,741 PointsCan't uderstand what's the problem
Hi, i got the "error: app.js:2 Uncaught TypeError: Cannot read property '0' of undefined at app.js:2" and i have no idea why i got it, here's my code and thanks for the time.
var color= $(".selected").css("background-color");
var context = $canvas[0].getContext('2d');
var $canvas = $("canvas")
var lastEvent
function changeColor() {
var r = $("#red").val();
var g = $("#green").val();
var b = $("#blue").val();
$("#newColor").css("background-color", "rgb(" + r + "," + g + "," + b + ")");
}
$(".controls").on("click","li", function() {
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
color= $(this).css("background-color");
});
$("#revealColorSelect").click(function() {
changeColor()
$("#colorSelect").toggle();
});
$("input[type=range]").change(changeColor);
$("#addNewColor").click(function() {
var $newColor = $("<li></li>");
$newColor.css("background-color",$("#newColor").css("background-color"));
$(".controls ul").append($newColor);
$newColor.click();
});
$canvas.mousedown(function(e) {
lastEvent = e;
}).mousemove(function(e){
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
lastEvent = e;
});
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Riccardo,
You have the order of the following 2 lines wrong:
var context = $canvas[0].getContext('2d');
var $canvas = $("canvas")
You first have to assign the canvas element to the $canvas
variable and then you can use it to get the context.