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
Gary Christie
5,151 PointsAnother JQuery question driving me crazy
Here are the instructions
Well done! Let's add one more jQuery event to our "destruction of Krypton" simulation. Krypton didn't just vanish, it exploded! Let's make it turn red.
$('div').hover(function(){ $('div').addClass('green'); });
Following the pattern we have been learning, we target Krypton, our $('div')
We then apply our hover event to our target.
Finally, we execute the code inside the function(){} which adds a class of green to our target.
Instructions
Below your .click() event code from the previous lesson, create a new selector $('div')
Apply a hover event to this selector and have it add the class red to Krypton, our div
Check out the example above!
Here is my code: $(document).ready(function(){ $('div').click(function() { $('div').hover(function(){ $('div').addClass('red'); }); }); }); This will not work for nothing. Maybe I'm wrong.
Now when I put in the index.html and add the class 'red' to the div then the planet turns red and I don't have to hover or anything to make that happen. When I hit save and Submit then it works. But its suppose to turn red when I hover over it not set the class in the div thru html.
2 Answers
B B
2,934 PointsYou embedded the hover function inside your click function. You need to have one click function and then below it create your hover function separately but still inside the jQuery(document).ready block.
Maor Tzabari
1,762 PointsYeah, it should be like that:
$(document).ready(function(){
$('div').hover(function(){
$('div').addClass('green');
});
});
But notice that after you hover that div the color stays, so you need to remove the class when the mouse leaves this element:
$(document).ready(function(){
$('div').hover(function(){
$('div').addClass('green');
});
$('div').mouseleave(function(){
$('div').removeClass('green');
});
});