0
$('.product-list').hover(
  $('.product-list-thumbnail ').css({'opacity': "0.2;"})
);

On hover of an element i'm trying to change the opacity of another element. I tried using CSS, but this is the only solution I could find.

My HTML looks like:

<article class="wrapper">
  <div class="product-list-thumbnail "><img src="" /></div>
  <div class="product-list">Product details</div>
</article>

As I have a lot of these on the page. How can I target the class .product-list-thumbnail that's within the container my current class is in?

Thanks!

9
  • that is not the proper way to use hover... Commented Dec 10, 2015 at 17:23
  • @epascarello it's the only way I could find to be able to toggle another element above the current one on hover Commented Dec 10, 2015 at 17:24
  • hover expects a function, that is not function Commented Dec 10, 2015 at 17:25
  • 3
    You could do this with CSS .wrapper:hover .product-list-thumbnail { opacity: 0.2; } but the element you're changing the opacity off must be a child of the one you're hovering over. Commented Dec 10, 2015 at 17:26
  • 1
    Hey, shouldn't the class name be "product-list-thumbnail" instead of ".product-list-thumbnail" ? Commented Dec 10, 2015 at 17:26

4 Answers 4

8

You're not properly using method hover. This method expects two functions, first is called on hover and second called on blur. Also you can use $.siblings to find .product-list-thumbnail.

$('.product-list').hover(
  function(){
   $(this).siblings('.product-list-thumbnail').css({'opacity': "0.2"}); 
  },
  function(){
   $(this).siblings('.product-list-thumbnail').css({'opacity': "1"}) ;
  }  
);
Sign up to request clarification or add additional context in comments.

Comments

1

I guess it's a misstyping but in your html syntax there is a dot. ".product-list-thumbnail" Try this.

 <div class="product-list">Product details</div>

Comments

0

fix the line <div class=".product-list-thumbnail"> replace with <div class="product-list-thumbnail">

..And here we are ;)

$('.product-list').hover(function(){
  $(this).parent().find('.product-list-thumbnail').css({'opacity': '0.2'})
},function(){
  $(this).parent().find('.product-list-thumbnail').css({'opacity': '1'})
});

Comments

0

First you could try and remove the period on the class to look like this <div class="product-list-thumbnail "> then I'd suggest a change the html code to wrap the thumbnail on the same element (better syntax) to something like

<div class="product-detail">
    <img src="" />
    Product detail information
</div>

So you can affect the element through css like this:

.product-detail:hover img {opacity: .2}

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.