6. Adding and removing cl@sses


#1

This code works in the editor, but Codecademy doesn't seem to this so ('oops, try again'). I have refreshed several times...

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


#2

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

It should be highlighted(small case l)


#3

Ah yes. That was just something I tried. But unfortunately that wasn't the problem :frowning:


#4

Oh actually, it is working now. Thanks! :slight_smile:


#5

Just wondering about best practices:

I solved this with the following inside the document.ready function:
'''
$('#text').on('click', function(){
$('#text').addClass('highlighted');
});

Is there any reason to do this via the event handler instead of the easier to type
click(function){
});
?
I would guess that it affords more flexibility long term, since one can then add a second parameter to specify the element to listen for inside a div.


#6

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