Jump to content

My stop autoplay button is not working

Litbelb

I have some music that will autoplay, some pretty simple HTML.

<audio id=autoplayaudio autoplay="true" src="URL">

Now, I have a button and some JavaScript that should make the music not play on auto.

<button onclick=stopAutoplay>

function stopAutoplay { document.getElementById("autoplayaudio").autoplay = "false" }

The problem: I am getting no errors, it's just that nothing happens at all. The music doesn't stop, and I'm not getting any errors that will tell me what's wrong.

I am fairly new to JavaScript, so I appreciate any help.

Thanks!

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Litbelb said:

The music doesn't stop, and I'm not getting any errors that will tell me what's wrong

You're just setting it not to autoplay, you're not stopping music that's already playing. Use e.g. element.pause() to pause playback.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Litbelb said:

I have some music that will autoplay, some pretty simple HTML.

<audio id=autoplayaudio autoplay="true" src="URL">

Now, I have a button and some JavaScript that should make the music not play on auto.

<button onclick=stopAutoplay>

function stopAutoplay { document.getElementById("autoplayaudio").autoplay = "false" }

The problem: I am getting no errors, it's just that nothing happens at all. The music doesn't stop, and I'm not getting any errors that will tell me what's wrong.

I am fairly new to JavaScript, so I appreciate any help.

Thanks!

 

 

Your syntax is incorrect, it should be

 

<button onclick="stopAutoplay()">  Stop </button>

function stopAutoplay() { document.getElementById("autoplayaudio").autoplay = false }

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, WereCatf said:

You're just setting it not to autoplay, you're not stopping music that's already playing. Use e.g. element.pause() to pause playback.

Can I have an example?

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, elpiop said:

 

Your syntax is incorrect, it should be

 


<button onclick="stopAutoplay()">  Stop </button>

function stopAutoplay() { document.getElementById("autoplayaudio").autoplay = false }

 

Ah. Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×