0

I'm trying to make it where when the user .mouseover() the .featured_products the .featured_products, and the .button will apply the CSS affect to the selected container. The problem i'm encountering is it changes the .css of both the .feature_products containers. I'd like it to only change the one that's being .mouseover(). I tried using $(this) but i'm not understanding it correctly.

$(".featured_products").mouseover(function(){
    $(".fp_button").css("background-color", "#00addc");
    $(".fp_button").css("color", "#FFFFFF"); 
    $(this).addClass("fp_hover");
  });
  $(".featured_products").mouseleave(function(){
    $(".fp_button").css("background-color", "white");
    $(".fp_button").css("color", "#000000")
    $(".featured_products").removeClass("fp_hover");
  });

Here is my Demo

5
  • is there a reason you're handling this with jQuery? Do you have access to the CSS? Commented Dec 10, 2015 at 21:13
  • Mostly because Jquery is the newest language i'm learning, so i thought it would be a easy way to achieve this affect. Could you give me a demo of the CSS alternative maybe on some simple <Div> boxes with background-colors? Thanks! :) Commented Dec 10, 2015 at 22:05
  • There: jsfiddle.net/4417zugn/36 it's very simple CSS, I would avoid jQuery for that. Commented Dec 10, 2015 at 22:16
  • Thanks a bunch. I had no idea CSS had the ability to do something like this. I thought it was limited to by it's own element. I will use this alternative instead. Atleast i learned both ways to-do this. Thanks a bunch! Is this something new with CSS? Commented Dec 10, 2015 at 22:21
  • It's not new, but IE6 (I forgot about IE7) had trouble with :hover on anything else than an achor tag (<a>). Commented Dec 10, 2015 at 22:31

4 Answers 4

3

You can use the second parameter in the selector to denote a parent, like:

$(".fp_button", this).css("background-color", "#00addc");

See it here: http://jsfiddle.net/4417zugn/31/


You can also do something like:

$(this).find(".fp_button")...

etc. There are many ways.

One thing I'd suggest is to change the class name instead of modifying individual CSS rules, like this: http://jsfiddle.net/4417zugn/33/


Last thing, this is all possible using only CSS, like this: http://jsfiddle.net/4417zugn/35/

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

2 Comments

Thanks A bunch! This helped me a- lot understanding how to use the This function.
You're welcome! I hope I covered all relevant scenarios.
1

There's no need to use jQuery to alter the CSS you can do that in the CSS itself using the :hover selector. You can then use jQuery to toggle the 'fp_hover' class.

$('.featured_products').hover(function(){
    $(this).toggleClass('fp_hover')
})

https://jsfiddle.net/Lozgnz84/

2 Comments

Why wouldn't you do the whole thing in CSS then?
Because it's not clear in the question what the 'fp_hover' class is being used for. It may not be for styling alone.
0

$(".fp_button") is common for both the divs; so instead of writing:

$(".fp_button").css("background-color", "white");

Write: $(this).find('.fp_button').css("color", "#FFFFFF");

Hence, your code becomes

$(".featured_products").mouseover(function(){
    $this = $(this);
    $this.find('.fp_button').css({"background-color":"#00addc", "color":"#FFFFFF"});          
    $this.addClass("fp_hover");
 });
$(".featured_products").mouseleave(function(){
  $this.find('.fp_button').css({"background-color":"white", "color":"#000000"});
  $this.removeClass("fp_hover");
});

demo here: http://jsfiddle.net/znnamrwn/

Comments

0

What you've described can be done without jQuery. If however you would like to use jQuery you could simply toggle a class on the product element.

$('.featured_products').on({
    mouseenter: function() {
        $(this).toggleClass('fp_hover');
    },
    mouseleave: function() {
        $(this).toggleClass('fp_hover');
    }
}, '.featured_product');

http://jsfiddle.net/bradlilley/uwxsr4hu


You can also do the above without jQuery by simple adding the following hover state in your css.

.featured_product:hover .fp_button {
    background: #f00;
    color: #000;
}

https://jsfiddle.net/bradlilley/9mwxo9o2/6/

Edit: You should also avoid using mouseover and use mouseenter instead.

Jquery mouseenter() vs mouseover()

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.