[SOLVED] Change div background on hover of another element

This is a discussion on "[SOLVED] Change div background on hover of another element" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Change div background on hover of another element are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Dec 7th, 2007, 22:15
Aso's Avatar
Aso Aso is offline
Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,322
Blog Entries: 2
Thanks: 9
Thanked 48 Times in 45 Posts
[SOLVED] Change div background on hover of another element

Is there a script out there that can dynamically change the background of a div to various images, depending on which element is 'hovered' or mouse-overed elsewhere on the page?

In short, I want a div at the bottom of my page to switch between preset background images based on the navigation tab at the top that the user hovers over

Many thanks

Alex
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Dec 7th, 2007, 22:34
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

Should be easy. Without loss of generality, let's say you want to change the background of <div id="swap"> when the mouse is over <div id="hover">. Use this javascript:

Code: Select all
swapImg("hover", "newBackgroundImage.png")

function swapImg(id, src) {
document.getElementById(id).onmouseover = function() {
document.getElementById("swap").style.backgroundImage = src
}
}
Something like that, anyway. I may have messed up the abstraction where you go into the second function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Dec 7th, 2007, 22:46
Aso's Avatar
Aso Aso is offline
Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,322
Blog Entries: 2
Thanks: 9
Thanked 48 Times in 45 Posts
Re: Change div background on hover of another element

Sorry Mike, no go. I wouldn't know what to change, I'm a javascript amateur Thanks for the quick response
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Dec 7th, 2007, 22:54
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

Quote:
Originally Posted by aso186 View Post
Sorry Mike, no go. I wouldn't know what to change, I'm a javascript amateur Thanks for the quick response
Then we can do the same thing without any abstraction. This should work "out the box"; I'll even comment it for you:
Code: Select all
/* 
"swap" is the id of the <div> whose background you want to change.
"hover" is an example of a <div> (or other element) that makes that change.
"hoverImg.png" is the background image that is used when you mouseover "hover".
You'll need to copy this code for each new background-changing item, so you'll have functions change2, change3, and so on.
*/

document.getElementById("hover").onmouseover = function(){ change1() }

function change1() {
document.getElementById("swap").style.backgroundImage = "hoverImg.png"
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Dec 7th, 2007, 23:00
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

Have you got a link to your web page so we can give you an example?

Basically what you would do is: (If anything is wrong, please correct me)
Code: Select all
<script type="text/javascript">

window.onload = swapImg("hover", "newBackgroundImage.png"); 

function swapImg(id, src) {
 document.getElementById(id).onmouseover = function() {
  document.getElementById(id).style.backgroundImage = src
 }
}

</script>
You would change the "hover" part to the ID of your div. You would change "newBackgroundImage.png" to the path to your image from this file (So it might be "../images/myimage.jpg")

You then place that whole piece of code somewhere in between the <head> </head> section of your webpage.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Dec 7th, 2007, 23:11
Aso's Avatar
Aso Aso is offline
Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,322
Blog Entries: 2
Thanks: 9
Thanked 48 Times in 45 Posts
Re: Change div background on hover of another element

Code uploaded at http://www.neilraymond.co.uk/test/test.htm

Just can't make it work

EDIT: I'm getting a javascript error
Code: Select all
document.getElementById(id) has no properties
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)

Last edited by Aso; Dec 7th, 2007 at 23:24.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Dec 7th, 2007, 23:28
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

As I said, I might have messed up the first (abstracted) version. Have you tried my second set of code?

You can also debug the code by placing the line alert("break") at various points. That tells you how far the code got.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Dec 7th, 2007, 23:35
Aso's Avatar
Aso Aso is offline
Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,322
Blog Entries: 2
Thanks: 9
Thanked 48 Times in 45 Posts
Re: Change div background on hover of another element

Tried it. No luck (www.neilraymond.co.uk/test/test2.htm)

EDIT: No alert when debugging either
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Dec 7th, 2007, 23:49
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

Right, I've had to change the code a bit. But it works now.

HTML: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Untitled 3</title>
    <script type="text/javascript">
    
    function swapImage(id, src) {

        document.getElementById(id).style.backgroundImage = "url('" + src + "')";
    }

     </script>
</head>

<body>

<a href="#" id="hoverLink" onmouseover="swapImage('swapDiv','tartan.png')">Hover Element</a>
<div id="swapDiv" style="width: 200px; height: 200px; border: 1px solid red;">Swapped BG</div>

</body>
</html>
I've stripped the handlers from the javascript and just have the function.
I then used a event handler on the link (onmouseover="swapImage('swapDiv','tartan.png')") to trigger the function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Dec 8th, 2007, 00:06
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Change div background on hover of another element

I almost never do this, but since you're so helpful to other members, here's something in return. Merry Christmas.

Working demo page.

*edit* maybe not so useful now. Oh well, it's the thought that counts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Dec 8th, 2007, 11:08
Aso's Avatar
Aso Aso is offline
Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,322
Blog Entries: 2
Thanks: 9
Thanked 48 Times in 45 Posts
Re: Change div background on hover of another element

Thank you Mike & Blake. Really appreciate it

Merry Christmas indeed!
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] isolate clicks on the background of an element ??? pesho318i JavaScript Forum 3 Nov 21st, 2007 13:01
[SOLVED] Change Input Textbox Background Color tejaxx JavaScript Forum 4 Nov 19th, 2007 20:16
How To Active Hover? Where I have to Change? opengiga Web Page Design 9 Oct 5th, 2007 05:45
change colour of text, on hover. Jason3107 Web Page Design 28 Sep 27th, 2007 23:10
a:hover background chnage gribble Web Page Design 1 Aug 12th, 2005 19:29


All times are GMT. The time now is 09:58.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42