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 trialBoris Kamp
16,660 PointsJavascript .hover .show and .hide issue
Hi guys! Working with this piece of code:
$(".grid-item").hover(
function() {
$( ".glyphicon.glyphicon-plus" ).show();
},
function() {
$( ".glyphicon.glyphicon-plus" ).hide();
})
});
I have multiple .grid-item on each page (grid layout) and it shows and hides the glyphicons on all of them if I hover over one of them. I need the glyphicon to only show on the current .grid-item that the user hovers over.
I've looked into .each and 'this' but I can't work it out.
How can I fix this? Thanks!
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Boris,
Can you show the html structure for one .grid-item
?
You'll most likely want to use $(this) in combination with jQuery's tree traversal methods http://api.jquery.com/category/traversing/tree-traversal/
This will allow you to target the specific glyphicon that belongs to the .grid-item
you're hovering over.
It depends on the relationship between the grid-item and the glyphicon.
Assuming the glyphicon is some descendant of .grid-item then this might work out for you:
$(".grid-item").hover(
function() {
$(this).find( ".glyphicon.glyphicon-plus" ).show();
},
function() {
$(this).find( ".glyphicon.glyphicon-plus" ).hide();
})
);
Marcelo Retana
2,534 PointsYou must use $(this)
Boris Kamp
16,660 PointsIm sorry but as mentioned above, I was unable to figure it out myself using the $(this) method, so do you mind being more specific?
Thank you!
Boris Kamp
16,660 PointsThanks! Thanks to your answer I was able to figure out the rest myself. Here is the code I used:
//Plus icon hover effect nieuws grid items
$(".grid-item").hover(
function() {
$(this).find( "span" ).show();
},
function() {
$(this).find( "span" ).hide();
}
);
note that in you code you had an }) to much, probably because you copied my code from above where I had it as well (-: Thanks!
Jason Anello
Courses Plus Student 94,610 PointsYou're welcome.
Thanks for the code correction. Yes, I copied and hadn't noticed it. :)