0

I'm trying to add a CSS class on hover to an element using jQuery. However, the class should only be applied to the closest element with a specific class so I tried using .next

When I add the .next to my code though the class isn't added anymore (it works without that part of the code). This is the code:

$j('.products_overlay').hover(function(){
    $j(this).next('.hover_text').addClass('overlay_text'); 
}, function(){
    $j(this).next('.hover_text').removeClass('overlay_text');
});

Any idea what I'm doing wrong?

This is the HTML

<div class="products_overlay">
    <a href="..." title="product1" class="product-image">
        <img src="...." alt="product1" class="hover_test" />
    </a>
    <p class="hover_text">Test</p>
</div>
8
  • 3
    Your HTML, please? Commented Jul 27, 2016 at 13:36
  • Some snippet or fiddle? Commented Jul 27, 2016 at 13:37
  • 1
    It sounds like .hover_text is not a sibling of .products_overlay so next() won't find it. We need to see your HTML to be able to help you fix this Commented Jul 27, 2016 at 13:39
  • without seeing html its hard to know, try $j(this).parent().find('.hover_text') Commented Jul 27, 2016 at 13:41
  • Just added the html Commented Jul 27, 2016 at 13:43

3 Answers 3

1

Should be

$j('.products_overlay .hoverText').addClass('overlay_text');

or

$j(this).find(".hoverText").addClass('overlay_text');

That's because next() doesn't look in the descendants, but in the next nodes.

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

Comments

1

Your problem is the next which only finds siblings

$j('.products_overlay').hover(function(){
    $j(this).next('.hover_text').addClass('overlay_text'); 
}, function(){
    $j(this).next('.hover_text').removeClass('overlay_text');
});

this in that case is the products_overlay div, so your .hover_text is a child and not a sibling.

To fix it use find:

$j('.products_overlay').hover(function(){
    $j(this).find('> .hover_text').addClass('overlay_text'); 
}, function(){
    $j(this).find('> .hover_text').removeClass('overlay_text');
});

Comments

0

Your jQuery code doesn't work as next() looks at siblings, yet .hover_text is a child of .products_overlay. As such you should use find(). You can also shorten the code using toggleClass():

$j(function($) {
    $('.products_overlay').hover(function(){
        $(this).find('.hover_text').toggleClass('overlay_text'); 
    });
});

That being said, given your HTML you don't need to use jQuery at all. You can achieve what you need in CSS alone:

.products_overlay:hover p {
  color: red; /* place the relevant hover styling in here */
}
<div class="products_overlay">
  <a href="..." title="product1" class="product-image">
    <img src="...." alt="product1" class="hover_test" />
  </a>
  <p class="hover_text">Test</p>
</div>

Comments

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.