1

Im not so good with JavaScript or jQuery, but I'm trying to get an image to show when I hover over the <div> element. The problem is that it is happening with the other ones too.

$(document).ready(function () { $("#event .event").hover(function () { $(".event img").show("slow"); }, $(".event img").mouseout(function () { $(".event img").hide("slow"); })); }); 

Html code:

<div id="event"> <div class="event"> <h4>name1</h4> <p>some text1</p> <img style='position:relative; display:none;' src='img1' alt='' /> </div> <div class="event"> <h4>name2</h4> <p>some text2</p> <img style='position:relative; display:none;' src='img2' alt='' /> </div> </div> 

But the image is coming on both, or all five if I have five.. Some help?

3 Answers 3

1

The second parameter of the $ function is often times used to define the context. Meaning, your selector will be used relative to a particular element, or selector.

$(".event") // finds all .event elements $(".event", this) // finds all .event elements within 'this' 

With this in mind, we can modify your code to look within the .event element currently being entered or existed to find the appropriate image:

​$("#event .event").on({ mouseenter: function(){ $("img", this).show(); }, mouseleave: function(){ $("img", this).hide(); } });​​​ 

This binds some logic to every element that matches the #event .event selector. Each one of these will have a handler bound to their mouseenter and mouseleave events. Anytime you hover one of those elements matched by the selector, we reference that particular element using the this keyword.

If you enter the first #event .event element on the page, this refers to that element. Likewise, if you exit that element, this refers to that element being exited. Recalling the first point of this response, that means $("img", this) will target every image within the element raising the mouseenter or mouseleave events to begin with.

Also, double-check your use of .hover(); this method takes two parameters, functions, that will be called when you mouseenter and when you mouseleave, respectively:

$("#event .event").hover( showImages, hideImages ); function showImages () { $("img", this).show(); } function hideImages () { $("img", this).hide(); } 

Or you could place those directly inside the .hover method as anonymous functions:

$("#event .event").hover( function(){ $("img", this).show(); }, function(){ $("img", this).hide(); } ); 
Sign up to request clarification or add additional context in comments.

2 Comments

@nodde You need to be more specific; what doesn't work? The above code works, I have tested it against your HTML.
yes, it working, but it dosent work on my site. It was working when i did not use @this . So i think there is somthing wrong on the site. But i dont know what it is.
0

Try this. IMO this solution is easier to understand.

$(document).ready(function () { $("#event .event").hover(function () { $(this).find('img').show(); }, function(){ $(this).find('img').hide(); }); }); 

Comments

0

You're hiding and showing all the images, you need to use the this keyword to target only the current element, not all of them, here is another simpler solution using toggle:

$(document).ready(function(){ $("#event .event").on('mouseenter mouseleave', function() { $('img', this).toggle(e.type=='mouseenter'); }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.