Current location: Hot Scripts Forums » General Web Coding » JavaScript » Change Div Background Onclick


Change Div Background Onclick

Reply
  #1 (permalink)  
Old 07-06-10, 02:31 PM
xavier039 xavier039 is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Change Div Background Onclick

I am relatively new to working with Javascript so I am hoping somebody can help me out here.

What I would like to do is have the div background change when a user clicks on another image or link.

I set up a few classes in CSS each with their own background image. Here is what I have but I am not sure what I am missing. Thanks so much!!

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>TheWebsite</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#hdr {
	height : 150px;
	width : 100%;
	background-image: url(gang/Bass.png);
	background-repeat: no-repeat;
  }
.hdr1 {
	height : 150px;
	width : 100%;
	background-image: url(gang/carl.png);
	background-repeat: no-repeat;
  }
.hdr2 {
	height : 150px;
	width : 100%;
	background-image: url(gang/Chet.png);
	background-repeat: no-repeat;
  }
.hdr3 {
	height : 150px;
	width : 100%;
	background-image: url(gang/Mel.png);
	background-repeat: no-repeat;
  }
</style>
 
</head>
<body >
<div id="hdr">Sometext</div>

<div><b>Alternate Background Images</b> -<br /> 
    <a href="#none" onclick="document.getElementById('hdr').className ='hdr1'">Option 1</a>
    <a href="#none" onclick="document.getElementById('hdr').className ='hdr2'">Option 2</a>
    <a href="#none" onclick="document.getElementById('hdr').className ='hdr3'">Option 3</a>
    <a href="#none" onclick="document.getElementById('hdr').className ='hdr'">Default</a>
  </div>
  
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 07-08-10, 11:45 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
works fine on Firefox 3.6.6 on Windows 7
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 07-09-10, 12:55 AM
drsim's Avatar
drsim drsim is offline
Newbie Coder
 
Join Date: Jul 2010
Location: UK
Posts: 45
Thanks: 1
Thanked 1 Time in 1 Post
tested and working fine on firefox 3.6 , IE 7, IE 8 . the only reason why it is not working on your side could be the correct image path.
Also give a default class to the div, because your JS code is only changing the class. otherwise use setAttribute function to add class attribute. hope this helps
Reply With Quote
Reply

Bookmarks

Tags
html, javascript


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox to change onclick event s1yfa1 PHP 0 06-25-10 04:48 PM
Content of a div affecting links in other divs? sdd CSS 5 03-27-10 02:37 AM
Space Above Form xavier039 CSS 1 07-13-09 10:52 PM
[SOLVED] Change content in a DIV with links in another DIV s1yfa JavaScript 3 05-04-08 04:10 AM
Converting to ems spyke01 CSS 0 11-09-07 02:48 PM


All times are GMT -5. The time now is 02:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.