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 Perform: Part 2

lordcozycat
lordcozycat
11,940 Points

Code appears to be identical, yet my page doesn't interact the same way as in the video.

Issue: Color in the "New Color" menu doesn't change while sliding, only after releasing the slider, unlike in the video. Happens very near the end of the video if you need to see what should happen.

Browser is Chrome, and I can't see what I've missed :/ Here's the code

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

//When clicking on control list items
$(".controls li").click(function() {

  //Deselect sibling elements
  $(this).siblings().removeClass("selected");

  //Select clicked element
  $(this).addClass("selected");

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

//When new color is pressed
$("#revealColorSelect").click(function() {
  //Show color select or hide
    changeColor();
    $("#colorSelect").toggle();
});

//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
$("input[type=range]").change(changeColor);

Color is not responding for me, even with the modification.

var color =$('.selected').css('background-color');

$('.controls li').click(function(){
  $(this).siblings().removeClass('selected');
  $(this).addClass('selected');
  color = $(this).css('background-color');
});
$('#revealColorSelect').click(function(){
  changeColor();
  $('#colorSelect').toggle();
});

function changeColor(){
  var r = $('#red').val();
  var g = $('#green').val();
  var b = $('#blue').val();
  $('#newColor').css('background-color', 'rgb(" + r + "," + g + "," + b + ")');
}
$('input[type=range]').on('input', changeColor);

Did I screw up somewhere? I've looked over it a dozen times now?

Stephen, in your css command, you have mismatching quotes. Each string should be surrounded by the same quotes, whether they are double or single quotes. So, your statement should look like this:

$('#newColor').css('background-color', "rgb(" + r + "," + g + "," + b + ")");

That's intentional. If you look at the highlighting of your version, you'll see that it doesn't work, because strings within a string need to have different quotes or be preceded by a "\" to be made part of the string.

Stephen, listen, those r g and b are variables and should not be within the string at all which is why they are not color coded like a string. I've not only done this project but made my version of this project completely kickass which you can check out here: http://marcusparsons.com/projects/sketchmeh. Trust me, bud, I know what I'm talking about.

Edit: Just to show you further about what's going on between what you posted and what I posted. Your css command is trying to set the background color to this literal string: 'rgb(" + r + "," + g + "," + b + ")' which is obviously not going to work. The correct css command (aka the one I gave you) is setting the background color this: 'rgb(r, g, b)' where r g and b are values. When you concatenate variables to strings, you need to place the variables and the operators outside of the string in order for the JavaScript compiler to know that you want the value of a variable there and that it needs to be concatenated, instead of just the letters r g b and some + signs. See what I'm saying?

Ohhh, yeahp. You got me there. Funny bit is it was another one of those "overthinking it into the wrong" things. Thanks bro!

It's okay, but please, man, in the future, at least try it out before saying it's not going to work lol. It's definitely possible for me to mess up and put up a quote where it doesn't belong or something of the like, so I'm never afraid of criticism. But unless it's glaringly obvious it is not going to work, you shouldn't just disregard it and say it's not going to. Anyways, no problem, man, and good luck with future coding! :D

Well, of course, it was thinking I had that section correct that was giving me the error. The human error, as always. =P

I definitely heard that! It happens man haha

1 Answer

Hi Bryan,

You want to listen for the "input" event instead. That one will go off repeatedly as you drag the slider. The "change" event only goes off once when you release the slider. The reason it worked in the video is because chrome had an incorrect implementation of the "change" event. It was acting the same way as the "input" event is supposed to act. It has since been fixed.

$("input[type=range]").on("input", changeColor);

lordcozycat
lordcozycat
11,940 Points

Hey, thanks so much! They really should add that to the teacher's notes.

Andrew Chalkley Is this something that can be added to the Teacher's Notes?

Thanks Andrew! That should help a lot.

This hasn't been updated, came here to state the same thing!

Christopher Bijalba, the video itself hasn't been updated because it would require re-shooting of the video, but it is in the Teacher's Notes.

Was experiencing the same problem. Thanks a lot !!

Hope it there will be a way that I do not come to here ..