2 Replies - 2009 Views - Last Post: 18 September 2012 - 06:16 AM

#1 monsterenergy22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 12

Adding audio to if statement

Posted 17 September 2012 - 04:20 PM

Hi everyone,

Im using java script for a simple if else statement and was trying to add some audio to it. Play one song when its right, and another if its bad. Here is what I have minus the actual if statements. I commented out the audio portion because that broke my code and couldn't get it working.

if (x is true)
		document.write("Good job!<br />");
		
			/*<audio controls = "controls" autoplay = "autoplay" loop ="loop" >
			<source src = "congrats.ogg" type = "audio/ogg" />
			<source src = "congrats.mp3" type = "audio/mpeg" />
			<source src = "congrats.wav" type = "audio/wav" />
			Your browser does not support the audio element
			</audio>*/
	else
		document.write("Boo!<br />");


Any help is appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding audio to if statement

#2 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

Re: Adding audio to if statement

Posted 17 September 2012 - 11:19 PM

First of all, the commented part was breaking your code because is not a valid javascript, you can't insert plain HTML into a script.
Possible solution:
- place the audio element inside a div and use CSS to hide it
- use javascript to select it and play the sound

You can find more about audio element here.

This post has been edited by StefanOnRails: 17 September 2012 - 11:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 monsterenergy22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 12

Re: Adding audio to if statement

Posted 18 September 2012 - 06:16 AM

View PostStefanOnRails, on 17 September 2012 - 11:19 PM, said:

First of all, the commented part was breaking your code because is not a valid javascript, you can't insert plain HTML into a script.
Possible solution:
- place the audio element inside a div and use CSS to hide it
- use javascript to select it and play the sound

You can find more about audio element here.

Thanks for the reply StefanOnRails.

After I posted my above code, I tried doing a
document.write();
and putting it in there but that didn't work.

After doing more research on the audio element, I was able to find a code snippet that did the trick. Thanks again.

http://www.position-...t-manipulation/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1