Al Hennessey
Al Hennessey

Reputation: 2445

Changing parent css on child hover

i have look around this forum a lot and can only find the opposite to my question, in other words i am looking to find how to change the parent div's background when the child div is hovered over, i can only find how to change the child div, when the parent is hovered over.

I have 1 parent div with an inner submit button:

<div class="ugd_ele_normal_base">
  <input name="ugd_1" type="submit" class="ugd_ele_normal"/>
</div><!--end ugd_ele_normal_base-->

What i want is for when the submit button is hovered over, the parent css changes background.

I have tried a few things, but nothing seems to work.

Thanks for the help

Upvotes: 2

Views: 9690

Answers (5)

adeneo
adeneo

Reputation: 318182

$('input.ugd_ele_normal').on({
    mouseenter: function() {
        $(this).parent().css('background', 'url(/folder/image1.jpg)');
    },
    mouseleave: function() {
        $(this).parent().css('background', 'url(/folder/image2.jpg)');
    }
});

or short(er) version:

$('input.ugd_ele_normal').on('mouseenter mouseleave', function(e) {
     $(this).parent()
            .css('background-color', e.type=='mouseenter'?'url(/folder/image1.jpg)':'url(/folder/image2.jpg)');
});

and to retrieve the old image:

var bgImg = $('input.ugd_ele_normal').css('background-image'),
    hvImg = 'url(/folder/image2.jpg)';

$('input.ugd_ele_normal').on('mouseenter mouseleave', function(e) {
     $(this).parent()
            .css('background-image', e.type=='mouseenter'?hvImg:bgImg);
});

or use classes:

.hoverClass {background: url(/folder/image2.jpg);}

-

$('input.ugd_ele_normal').on('mouseenter mouseleave', function() {
   $(this).toggleClass('hoverClass');
});

Upvotes: 5

bengreenier
bengreenier

Reputation: 285

i would define some jquery to do this, and use a custom class.

//when the document is loaded, execute the following code
$(document).read(function(){

//on this elements hover, ...
$('.ugd_ele_normal').hover( function(){
    //we add the class black to the parent on hover
  $(this).parent().addClass("black");
},
function(){
   //and remove it on exit hover.
   $(this).parent().removeClass("black");
});
});

​ can be seen in action here: http://jsfiddle.net/xBuyC/

Upvotes: 1

Stone
Stone

Reputation: 2668

I would use jQuery & CSS for this:

CSS

.black {
     background-color: #000000;
}

jQuery

$(function() {
   $('.ugd_ele_normal').hover( function(){
      $(this).parent().addClass("black");
   },
   function(){
      $(this).parent().removeClass("black");
   });
});

Upvotes: 8

Sinan AKYAZICI
Sinan AKYAZICI

Reputation: 3952

You can do this way.

$("#ugd_1").hover(function(){
   $(this).parent().css("background","red");
},
 function(){
      $(this).parent().css("background","none");
 });

​ ​ Live Demo :

http://jsfiddle.net/Z4QDC/2/

Upvotes: 2

juan.obando
juan.obando

Reputation: 209

Try:

$('input[name="ugd_1"]').hover(function() {
    $(this).parent().css('background-color', 'COLOR');
});

Hope it helps.

Upvotes: 0

Related Questions