/    Sign up×
Community /Pin to ProfileBookmark

getElementById() & <bgsound>

Hey!

Can somebody help me? I would like to change the background-sound in my webpage…

[CODE]if (radio==”on”) {
document.getElementById(“sound”).src=’sound/sound1.mp3′;
}[/CODE]

…and between the <head>, I have…

[code=html]<bgsound src=”#” id=”sound” loop=1 autostart=”true” />[/code]

…but that will give my browser an error like…

[QUOTE]

getElementById(…) is null or not an object

[/QUOTE]

Please help me!..

//Christoffer

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@RobDavidMay 21.2007 — it can't find your bg because it's located inside <head> and not <body>

try sticking it in the body
Copy linkTweet thisAlerts:
@Totte_chauthorMay 21.2007 — I had tested that too...

It will not work :S
Copy linkTweet thisAlerts:
@RobDavidMay 21.2007 — so, this doesn't work for you???

<html>

<body>

<bgsound src="#" id="soundeffect" loop=1 autostart="true" />


<script type='text/javascript'>


function playsound(soundfile){

if (document.all){

var soundEffect = document.getElementById("soundeffect");

soundEffect.src="" //reset first in case of problems

soundEffect.src=soundfile

}

}


</script>

<input type='button' onclick="playsound('yoursoundfile.wav');" value='play sound' />

</body>

</html>
×

Success!

Help @Totte_ch 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.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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