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

Kavita Bargota
Kavita Bargota
2,741 Points

how to add a start/stop button using Javascript. Anyone know the code?

<hr> <!--Buttons--> <center><a href="javascript:DennisBrown();"><img src="Dennis-Brown1.jpg" alt="DennisB"></a></center> <hr> <!--Audio Files--> <audio id="DennisBrown" src="rockingtime" preload="auto"></audio>

  <!--Javascript-->

<script type="text/javascript"> function DennisBrown() { document.getElementById('DennisBrown').play(); } </script>

does anyone know how to add a start and stop button? I have used the image and the button to play the audio file but now need a stop too otherwise the song keeps playing

thanks

Kavita

1 Answer

Steven Parker
Steven Parker
231,236 Points

It looks like you already have the start code.

To start playing, you call the .play() method on the audio object as in your code above.

To stop it, just call the .pause() method on the same object.

For more details, look at the documentation for HTMLMediaElement — most of the functional methods are inherited from it.

Kavita Bargota
Kavita Bargota
2,741 Points

Hi Steven

Thank you for replying. The link for further details is not letting me access anything?

Ok great, so I have the code for the start. I can see the .play() is already on there...where would I have to place the .pause()?

thanks

Steven Parker
Steven Parker
231,236 Points

You would create another function, just like the one you have above, but of course with a different name. The contents of the function would be the same except that where the current one says "play" the new one will have "pause". This will be the function you will call from your stop button.

There was a problem with the link, try it again now.

Kavita Bargota
Kavita Bargota
2,741 Points

Hi Steven

thank you this makes sense now. The link is working now too thank you.

so I did what you said and it still does not allow me to stop the audio. I am using the actual image as the button (I have not implemented an actual start/stop button feature). So clicking the image plays the song, I wanted the song to pause when clicked again..... <!--Buttons--> <center><a href="javascript:DennisBrown();"><img src="Dennis-Brown1.jpg" alt="DennisB"></a></center> <hr> <!--Audio Files--> <audio id="DennisBrown" src="rockingtime" preload="auto"></audio> <!--Javascript--> <script type="text/javascript"> function DennisBrown() { document.getElementById('DennisBrown').play(); } function pause() { document.getElementById('DennisBrown').pause(); } </script>

Steven Parker
Steven Parker
231,236 Points

When posting code, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: And if you're using the workspaces, you can make a snapshot of your workspace and post the link to it here.

But I don't see anything in your new code that would call the new pause() function. You might try adding a separate button for pausing first, since that's easier. Later you can work on making one button switch behaviors.

Some of these basic program structure tasks might be less confusing after you take some of the courses in the Beginner JavaScript track.

Kavita Bargota
Kavita Bargota
2,741 Points

(''') <hr> <!--Buttons--> <center><a href="javascript:DennisBrown();"><img src="Dennis-Brown1.jpg" alt="DennisB"></a></center

  <hr>
 <!--Audio Files-->

<audio id="DennisBrown" src="rockingtime" preload="auto"></audio>

  <!--Javascript-->

<script type="text/javascript"> function DennisBrown() { document.getElementById('DennisBrown').play(); } function pause() { document.getElementById ('DennisBrown').pause(); }

</script> (''')

Hi Steven, thanks. I new to this hence the mix up in posting the code but thanks for letting know. I have tried adding another button but that doubles my image. I have tried adding another function too but still no joy. thank you

Steven Parker
Steven Parker
231,236 Points

You already added the new pause function, and it looks good. But you're not calling it from anywhere yet.

If you don't want another image button, how about a plain one?

  <br><button onclick="pause()">Stop</button>

And for code quoting, use 3 accents :point_right: ```, not apostrophes :x::point_right: '''. And don't enclose them in parentheses.

Kavita Bargota
Kavita Bargota
2,741 Points

Hi Steven,

thank you for the reply.

I am taking your advice and added an actual stop button instead of another image for now. I think you are also correct in saying that I need to do more of the tutorials first! :)