/    Sign up×
Community /Pin to ProfileBookmark

Can’t get my code to work to play a sound when I click on an image

Hey all, was hoping I could get some help here, trying to get some sound to play when I click parts of an image. Here’s some of my code, hopefully you can help me find out what’s wrong

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Virtual Drums</title>
<link href=”wrapper.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript”>
function playSound(soundobj) {
var thissound=document.getElementById(sound1);
thissound.Play();
}
</script>
</head>

<body>
<embed src=”./MusicalNotes/sound1.wav” width=0 height=0
enablejavascript=”true” autostart=false id=”sound1″>

<img src=”Background_1.jpg” width=”800″ height=”200″ border=”0″ usemap=”#Map” /> <img src=”drum.jpg” width=”800″ height=”600″ border=”0″ usemap=”#Map2″ />
<map name=”Map2″ id=”Map2″>
<area shape=”rect” coords=”144,84,195,574″ />
<form>
<input type=”button” value=”Play Sound1″
onClick=”playSound1(‘sound1’)”>
</form>
<area shape=”rect” coords=”202,89,245,561″ href=”#” />
<area shape=”rect” coords=”254,90,297,514″ href=”#” />
<area shape=”rect” coords=”306,91,348,476″ href=”#” />
<area shape=”rect” coords=”359,93,396,427″ href=”#” />
<area shape=”rect” coords=”408,97,445,401″ href=”#” />
<area shape=”rect” coords=”455,98,484,372″ href=”#” />
<area shape=”rect” coords=”494,93,523,344″ href=”#” />
<area shape=”rect” coords=”534,98,563,316″ href=”#” />
<area shape=”rect” coords=”577,89,599,294″ href=”#” />
<area shape=”poly” coords=”610,91,608,286,632,285,636,93″ href=”#” />
<area shape=”poly” coords=”643,91,641,267,665,269,667,90″ href=”#” />
</map>
<map name=”Map” id=”Map”>
<area shape=”rect” coords=”73,149,217,194″ href=”#” />
<area shape=”rect” coords=”238,152,419,196″ href=”#” />
<area shape=”rect” coords=”436,151,755,196″ href=”#” />
</map>

</body>
</html>
</div>

Thanks a lot for any help you may be able to give!

I placed an image of drums, and I made hotspots on the drums and I’d like that when I click each individual drum, a sound plays.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoApr 30.2012 — have a look at your onclick...

[CODE]<input type="button" value="Play Sound1" onClick="playSound1('sound1')">[/CODE]

it passes the string "sound1" to the function

the function receives it here

[CODE]function playSound(soundobj) {
var thissound=document.getElementById(sound1);
thissound.Play();
}[/CODE]


and says right, soundobj now equals the string "sound1"

you then tell it to look for an element with an id equivalent to the [B]variable[/B] sound1 (which doesn't exist) so it fails.

I suspect your function would look better like this:

[CODE]function playSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.Play();
}[/CODE]
×

Success!

Help @12084182 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 4.20,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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