2

I'm learning jquery and I've been banging my head against this for a few days. I'm trying to create a page that has one large featured image and 4 small thumbnails beneath it. When the user hovers over the thumbnail, I need to change the featured image.

I'm having trouble expressing that when mouse hovers over A, toggle B. But when mouse hovers over C, then toggle D. What's currently happening is that all pshow classes toggle at the same time. Should I be using $(this) to toggle the current element? Should I be using a variable?

I've been searching stackoverflow for a similar question, but I haven't been able to find anything. Sorry if this is a duplicate. Is this the right approach??

JQUERY

<script type="text/javascript"> 
    $(document).ready(function () {
        $('.hover').mouseenter(function() {
        $('.pshow').toggle();
        });
    });
</script>

HTML

<div id="story1">
    <a href="#"><h2 class="hover">Story #1 Text</h2></a>
    <img class="pshow" style="display:none" src="#" >
</div>


<div id="story2">
    <a href="#"><h2 class="hover">Story #1 Text</h2></a>
    <img class="pshow" style="display:none" src="#" >
</div>


<div id="story3">
    <a href="#"><h2 class="hover">Story #1 Text</h2></a>
    <img class="pshow" style="display:none" src="#" >
</div>
1

3 Answers 3

2

I would do it like this...

Put the class into the div, instead of the H2

<div class="hover"  id="story1">

Then your Jquery...

 $('.hover').mouseenter(function() {
     $('.pshow').hide();
    $(this).find('.pshow').toggle();
    });

DEMO HERE

Sign up to request clarification or add additional context in comments.

Comments

2

You can use pure CSS for that

For that to work you would need to remove the inline style and set the property in CSS.

As inline styles has more specificity compared to a style defined in CSS.

HTML

<div id="story1">
    <a href="#"><h2 class="hover">Story #1 Text</h2></a>
    <img class="pshow"  src="#" />
</div>

CSS

img{              // .pshow
    display: none;
}

a:hover + img {  // a:hover + .pshow
    display: block;
}

Check Demo

1 Comment

Uh...that wont work...it will still show all....but also, just tried it in jsFiddle. Didnt work
0

You need to change your selector to select only one element with the class pshow. You can do that like this:

<script type="text/javascript"> 
$(document).ready(function () {
    $('.hover').mouseenter(function() {
         $(this).closest('div').find('img.pshow').toggle();
    });
});
</script>

2 Comments

at least explain it so he learns how to do it again instead of just copy and pasting
parents() would be a bad option... if there is a surrounding div over all the story divs... it would select all the pshow images. you should use closest() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.