You do not need # or . before your class


#1

This instruction is included but I'm a little puzzled by it.

Remember: You aren't selecting anything, you are modifying your element. This means that you do not need # or . before your class.

It seems to say that you don't need a # before the item ID or a "." before the class name if you're not selecting something and just modifying it.

This works:

$(document).ready(function() {
    $('#text').addClass("highlighted");
});

This does not work:

$(document).ready(function() {
    $('text').addClass("highlighted");
});

Returns error: Oops, try again. It looks like your #text doesn't have the class .highlighted. Check the Hint if you need help!


#2

Hi James,

So when we're selecting an element, there are three main ways we can select it.

<span id="fancy"></span>
<div class="container"></div>
<p></p>

By id (#), class (.), or tag name. So, to select each of those ↑ elements in order, we could do this:

$('#fancy')
$('.container')
$('p')

Now, in a real world situation, we might have the same class name as tag name - how does the computer know which to select? That's what the character in front of the (id, class, or tag) name represents.

So in this case, by putting a hash sign (#) in front of your id, you're telling the computer to select the element with the id="text" attribute. Simply using $('text') selects the - nonexistent - <text></text> HTML element.

I hope this helps, please let me know if you have any more questions :slight_smile:


#3

So what does this instruction mean? It was also mentioned in the quiz.

Remember: You aren't selecting anything, you are modifying your element. This means that you do not need # or . before your class.


#4

@textrockstar34699 Selecting, I explained above. Modifying, they mean when you're using addClass(), removeClass(), or toggleClass(), and I explained why those don't need a dot (.) in front of the class name already, over on this post:


#5

That totally makes sense (of course).