Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This should do it:</p> <pre><code>$('.contemp').hover(function(){ $(this) .closest('.boxShadow') .children('.normalPic').hide().end() .children('.hoverPic').show(); }); $('.trad').hover(function(){ $(this) .closest('.boxShadow') .children('.normalPic').show().end() .children('.hoverPic').hide(); }); </code></pre> <p>This will find the closest ancestor with class <code>.boxShadow</code> and from there finds its children <code>.normalPic</code> and <code>.hoverPic</code> and changes their visibility. Note how you can use method chaining to avoid searching for the same elements over and over again.</p> <p>Also note that the same function will be called when you leave the element. Instead of <code>hover</code>, you might just want to use <code>mouseenter</code>.</p> <hr> <p>Without repeating code, but maybe less intuitive:</p> <pre><code>$('.contemp, .trad').mouseenter(function(){ var is_contemp = $(this).hasClass('contemp'); $(this) .closest('.boxShadow') .children('.normalPic').toggle(!is_contemp).end() .children('.hoverPic').toggle(is_contemp); }); </code></pre> <hr> <p><strong>Reference:</strong> <a href="http://api.jquery.com/mouseenter/" rel="nofollow"><code>mouseenter</code></a>, <a href="http://api.jquery.com/closest/" rel="nofollow"><code>closest</code></a>, <a href="http://api.jquery.com/children/" rel="nofollow"><code>children</code></a>, <a href="http://api.jquery.com/toggle/" rel="nofollow"><code>toggle</code></a>, <a href="http://api.jquery.com/end/" rel="nofollow"><code>end</code></a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload