/    Sign up×
Community /Pin to ProfileBookmark

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!!

[code=html]<!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>[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@adamouJul 06.2010 — What you might want to do instead is

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

&lt;/head&gt;
&lt;body &gt;
&lt;div id="hdr" class="hdrDefault"&gt;Sometext&lt;/div&gt;

&lt;div&gt;&lt;b&gt;Alternate Background Images&lt;/b&gt; -&lt;br /&gt;
&lt;a href="#none" onclick="document.getElementById('hdr').className ='hdr1'"&gt;Option 1&lt;/a&gt;
&lt;a href="#none" onclick="document.getElementById('hdr').className ='hdr2'"&gt;Option 2&lt;/a&gt;
&lt;a href="#none" onclick="document.getElementById('hdr').className ='hdr3'"&gt;Option 3&lt;/a&gt;
&lt;a href="#none" onclick="document.getElementById('hdr').className ='hdrDefault'"&gt;Default&lt;/a&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @xavier039 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...