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

JavaScript

Jquery hovering an element

This is the code i have, and i would like to show some "info" when i hover a specific element, but when i do it all the elements do a fadeIn at the same time.

Andrew Chalkley

<div class="col-4 item">
            <div class="info">
                <h3> BRANDING </h3>
                <p> Author: </br> Genesis Rosario. </p>
            </div>
            <img src="images/gallery/item_1.jpg"/>
        </div>
        <div class="col-4 item">
            <div class="info">
                <h3> BRANDING </h3>
                <p> Author: </br> Genesis Rosario. </p>
            </div>
            <img src="images/gallery/item_2.jpg"/>
        </div>
        <div class="col-4 item">
            <div class="info">
                <h3> BRANDING </h3>
                <p> Author: </br> Genesis Rosario. </p>
            </div>
            <img src="images/gallery/item_3.jpg"/>
        </div>
$( document ).ready(function() {
    console.log( "ready!" );
    $(this).on("mouseenter", function(){
        $(".info").fadeIn(1000);
    });
    $(this).on("mouseleave", function(){
        $(".info").fadeOut(1000);
    });
});

hum... the code ain't showing as i would wish even when i added the ``` at the beginning and at the end....

I fixed that for you.. You weren't quite doing it right. You're supposed to put the 3 backticks, then, without any spaces, the name of the language in all lowercase. Then you're supposed to write your code on a new line. When you're done with your code, make another new line, and then put the 3 backticks. You're also supposed to put at least one blank line of padding before or after (depending on whether if it's at the beginning or end of your code) any line that has 3 backticks. If you want to see exactly how it works, just hit the "Edit" button on your question, and my edits will show up

Hi Anthony,

I fixed the formatting for you. You have to put the backticks on their own line.

Are you trying to hover over the image and then the "info" div fades in?

3 Answers

Hi Anthony,

You can use $(this) inside the two handlers to refer to the specific .item div that was interacted with. Using that, you would then need to select the first child (.info) of that particular .item div. You're selecting all .info elements and fading them.

$(".item").on("mouseenter", function(){
  $(this).children('.info').fadeIn(1000);
});

$(".item").on("mouseleave", function(){
  $(this).children('.info').fadeOut(1000);
});

.children() will select all the direct children of the .item div and then optionally filter the selection. In this case, you want to filter it down to only the .info div.

I don't know if you're interested but you should be able to handle this entirely with css using transitions.

That would be because in this case, this is literally the entire document. If you want to have only specific elements hover, you need to be more specific with your selector

$( document ).ready(function() {
    console.log( "ready!" );
    $(".item").on("mouseenter", function(){
        $(".info").fadeIn(1000);
    });
    $(".item").on("mouseleave", function(){
        $(".info").fadeOut(1000);
    });
}); 

even with this the 3 element will fadeIn together, is there a way to convine (this) with .item?

my logic says: when hover THIS item FadeIn this Info.

*EDIT:

tried this:

(".item", this)

Didn't Work...