HTML Audio & Video
HTML DOM Audio autoplay Property
The autoplay property sets or returns whether a media element should start playing as soon as it has loaded enough data. It reflects the autoplay HTML attribute and is a boolean.
Definition and Usage
When autoplay is true, the browser attempts to begin playback automatically without waiting for the user to press play. The property directly reflects the presence of the autoplay attribute on the element, so reading it tells you the setting and assigning a boolean adds or removes the attribute.
Setting autoplay = true is a request, not a guarantee. Modern browsers block autoplay with sound but generally allow muted autoplay, so combining autoplay with muted is the reliable pattern for background and hero videos.
Syntax
// Get the value
let auto = mediaElement.autoplay;
// Set the value
mediaElement.autoplay = true;Example
This runnable page enables autoplay on a muted video from JavaScript and reports the property value. Muted autoplay is normally permitted, so the clip should start on its own.
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>autoplay Demo</title></head>
<body>
<h2>Muted autoplay via JavaScript</h2>
<video id="myVideo" width="320" muted controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
<p><button onclick="check()">Read autoplay</button></p>
<p id="output"></p>
<script>
const video = document.getElementById("myVideo");
video.autoplay = true; // enable autoplay in code (kept muted)
function check() {
document.getElementById("output").textContent =
"autoplay = " + video.autoplay + ", muted = " + video.muted;
}
</script>
</body>
</html>More Examples
You can flip autoplay off before load to prevent a clip from starting automatically.
<!DOCTYPE html>
<html>
<body>
<video id="v" width="320" controls autoplay muted>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
<p>
<button onclick="v.autoplay = false">Disable autoplay</button>
<button onclick="report()">Show value</button>
</p>
<p id="msg"></p>
<script>
const v = document.getElementById("v");
function report() {
document.getElementById("msg").textContent = "autoplay = " + v.autoplay;
}
</script>
</body>
</html>Setting autoplay = true is not a guarantee. If the browser's autoplay policy blocks playback (for example unmuted media without user interaction), it silently ignores the request and the media stays paused.
Key Takeaways
- autoplay is a boolean that reflects the autoplay HTML attribute.
- Reading it returns the current setting; assigning adds or removes the attribute.
- Browsers block autoplay with sound but usually allow muted autoplay.
- Combine autoplay + muted (+ loop) for reliable background videos.
- A true value is only a request; the autoplay policy can still block playback.
