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

Why does this space in the jQuery/JavaScript code matter?

While doing the challenge to create a simple drawing tool in the JQuery Basics course, I've run into what seems to be an interesting and confusing (to me) problem.

It deals with this line of code:

$("input[type=range]").change(createNewColor);

It seems to make a huge difference if there is a space between ``` input

and 

[type=range]

That is, if there IS a space, the code doesn't run. If there is NOT a space, the code runs.

I'm wondering....why? (Spaces don't usually affect Javascript code, right?)

Here's my incomplete and poor understanding of what this line of code is supposed to do: make it so that the RGB values change as you move the color sliders back and forth. I think I'm missing something obvious, though!

Would someone please clear this problem up for me?

Thanks y'all!

--Rose

2 Answers

David Bath
David Bath
25,940 Points

The code inside $(...) is a CSS selector. It selects the range input that you want to change the value of. According to CSS rules there shouldn't be a space between the element

input

and the attribute selector

[type=range]

Ah, OK. So what I'm not understanding is how CSS selectors work. Thank you very much for the reply!