Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jason Ares
Jason Ares
1,183 Points

I can't get my javascript function to work. What am i missing?

<!--Practice--> <!DOCTYPE html> <html lang="eng"> <head> <meta charset="utf-8"> <meta name="viewport" content"width=device-width, inital scale=1, minimum scale=0.5, maximum scale-0.5, minimal ui"> <title> Laura Sounds</title> <!--sytle sheet link--> <link href="css/style.css" rel="stylesheet"> </head>

<body>

<!--Laura Image--> <img src="images/Laura.jpg" class"Laura" alt="Laura">

<!-- Buttons--> <a href="javascript:Angry();"><img src="images/Anger.jpg" alt="anger"</a> <a href="javascript:Crying();"><img src="images/Sad.jpg" alt="sad"</a> <a href="javascript:Happy();"><img src="images/smile.png" alt="happy"</a>

<!--Sound--> <audio id="Angry" src="sounds/Angry.wav" preload="auto"></audio> <audio id="Crying" src="sounds/Crying.wav" preload="auto"></audio> <audio id="Happy" src="sounds/Happy.wav" preload="auto"></audio>

<!--Javascript--> <script type="text/javascript" function Angry() { document. getElementById('Angry').play(); } function Crying() { document. getElementById('Crying').play(); } function Happy() { document. getElementById('Happy').play(); } </script> </body> </html>

The audio and image links seem to all be working.

1 Answer

Owa Aquino
Owa Aquino
19,277 Points

Hi Jason,

The only problem I can see is that you having spaces before the getElementById. Try fixing them and come back to me if it doesn't work.

Try this. I just remove the space between document. and getElementByID

function Angry() { 
    document.getElementById('Angry').play(); 
} 

function Crying() { 
    document.getElementById('Crying').play(); 
} 

function Happy() { 
    document.getElementById('Happy').play(); 
} 

Cheers!

Owa

Oliver Duncan
Oliver Duncan
16,642 Points

You're also missing a few closing brackets for your img and script tags...

Jason Ares
Jason Ares
1,183 Points

You're the best! also I was missing some brackets so thank you Oliver Duncan as well.