6/14, i got it but i dont know how


#1

hello, i got 6/14 but i dont know how i did it. this is my code (in the script.js tab) :

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

i dont understand how it worked because i didnt get the thing highlighted and it didnt show i could click it. please someone help me figure it out. thank you


#2

Hi Falsenajnaj,

Your code looks fine, and worked for me. Are you sure you're clicking on the second (dark blue) <div>?
Or, it may be that your screen just isn't showing the color change properly. Try adding background-color: red !important; to the .highlighted rule in your CSS file (it's at the bottom) and see if it changes then :)


#3

I have the same problem in this lesson. I tryed to add the suggested rules to .highlighted class but nothing changes. Can someone help me, also?


#4

@lenna85 Is your code exactly the same as @falsenajnaj's?


#5

Excuse me I didn't wrote the same code @falsenajnaj code. I was wrong: instead of addClass I wrote FUNC.. I'm sorry. Thank you @zystvan


#6

Hello, I have written the same code as falsenajnaj and have found that my code does not work at all. Does anyone have any idea to why this keeps happening? (happened in a previous exercise)


#7

@harpreetb17 Have you tried refreshing the page and switching browsers?


#8

Yeah I tried refreshing the page and a different browser and its just not working :frowning:
this is my code
$(document).ready(function() {
$('#text').click(function() {
$(this).addClass('.highlighted');
});
});

am i missing something?


#9

@harpreetb17 Yes, add/toggle/removeClass() all take just the name of the class. Normally, when selecting something with jQuery, you could select:

$('#example') // an #id
$('.example') // a .class
$('example') // a tag name

So some distinction is necessary, especially when you've got the same #id as .class or something. With add/toggle/removeClass() though, the computer knows it's dealing with a .class, so it doesn't need that dot (.) to distinguish it from something else.

TL;DR: Line 3 shouldn't have a dot before higlighted.


#10

i have written below code .. but its not working.. can somebody please give some input on this.

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


#11

@rubysurfer62475 Your code passed fine for me, try refreshing the page, and make sure you're clicking on the dark blue <div> in the full view window.
Next time, please create a new topic.


#12

its your "addClass" line!

the call of ".addClass()" knows that the name inside the brackets is a class therefore you do not have to put a "." to identify it.

Your code should now work:

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

Hence: ".addClass(.highlighted)"
changed to : ".addClass(highlighted)"

:smiley:


#13

Erase all the jquery code then write it over. Thats what I had to do as I had the exact same problem as you. Then it fixed the problem.


#14

$(document).ready(function() {

$('#text').addClass('highlighted');

});


#15

Erase all the jquery code then write it over. Thats what I had to do as I had the exact same problem as you. Then it fixed the problem.

That works!


#16

The reason it didn't work/let you click the blue is because your function is set to "click" meaning that the class will only be highlighted IF yyou click the blue box. Once you click it, it will become highlighted.

Try using "mouseenter" instead of "click".


#17

I tried your code, that is how I figured it out.


#18

the code SCRIPT.JS its ok, but you must add the class in HTML in ID TEXT