Adding classes and selectors, how it works?


#1


Theres a note that is confusing or I don't understand why is in the exercise, because in every way in order to obtain a correct answer I have to write # to select the id in this case. the note is this: "Remember: You aren't selecting anything, you are modifying your element. This means that you do not need # or . before your class."

So, the next code doesn´t work, why is that?

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


#2

^Here you won't need to add . to specify as addClass is used only for adding classes and nothing else.

^ Here you need to add # otherwise it will look for a text tag, so you need to add # to show its an id.


#3

I think I got it, I don't need to select "highlighted", because that is the class I'll modify, that's correct?, I was confused about selecting the element, and that element ("#text") has to be selected in order to trigger the action.

Thank you very much for the answer.

Then the correct answer is:

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

#4

Yes correct to everything you said :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.