CSS jQuery Change Image in Div on Hover

How do you change the image in a div with jQuery when another div is hovered?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim RiddlesPrepress/OMS SpecialistCommented:
Given your question, it would be similar to the following:

HTML
<div id="hoverDiv">Hover here to change an image elsewhere</div>
<div id="targetDiv"></div>

Open in new window


JavaScript
$("#hoverDiv").hover(function() {
	$("#targetDiv").css({"background-image":"url(http://www.gowhiteoak.com/images/general/WO_logo_web.png)"});
});

Open in new window


CSS
div {
    width:400px;
    height:200px;
    border:1px black solid;
    margin:10px;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brant SnowCommented:
Jim Riddles is correct, however you may wish to do this through classes
HTML
<div id="hoverDiv">Hover here to change an image elsewhere</div>
<div id="targetDiv"></div>

Open in new window


Javascript
$("#hoverDiv").hover(function() {
$("#targetDiv").addClass("myclass");
$("#targetDiv").removeClass("myclass");
});

Open in new window


<style>
.myclass
{
background-image:url(http://www.gowhiteoak.com/images/general/WO_logo_web.png);
}
</style>

Open in new window

0
Julian HansenCommented:
Here is way of doing it with CSS
CSS
.hoverme:hover + br + div.switch-image {
  background:url(images/foreground1.jpg) no-repeat;
}
.switch-image {
  width: 132px;
  height: 150px;
  background: url(images/foreground1.png) no-repeat;
}

Open in new window

HTML
  <div class="hoverme">Hover over me</div>
  <br/>
  <div class="switch-image"></div>

Open in new window

A bit more restrictive than the jQuery version - in that you have to expressly list the path from the hover div to the image and if it is backwards it won't work - but if the div's are adjacent or close then this is a quick way of doing it.
Working sample here
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.