60

I am looking for a way to show or hide HTML5 video controls at will via javascript. The controls are currently only visible when the video starts to play

Is there a way to do this with the native video controls?

I'm using google chrome browser.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Weatherman
  • 755
  • 1
  • 5
  • 6
  • Removing the controls hides the audio all together, so you will need to add the `autoplay` attribute or play the audio programmatically. – Patrick May 12 '16 at 08:39

3 Answers3

79
<video id="myvideo">
  <source src="path/to/movie.mp4" />
</video>

<p onclick="toggleControls();">Toggle</p>

<script>
var video = document.getElementById("myvideo");

function toggleControls() {
  if (video.hasAttribute("controls")) {
     video.removeAttribute("controls")   
  } else {
     video.setAttribute("controls","controls")   
  }
}
</script>

See it working on jsFiddle: http://jsfiddle.net/dgLds/

jessegavin
  • 74,067
  • 28
  • 136
  • 164
9

Here's how to do it:

var myVideo = document.getElementById("my-video")    
myVideo.controls = false;

Working example: https://jsfiddle.net/otnfccgu/2/

See all available properties, methods and events here: https://www.w3schools.com/TAGs/ref_av_dom.asp

Jakob Sternberg
  • 1,758
  • 14
  • 12
3

CARL LANGE also showed how to get hidden, autoplaying audio in html5 on a iOS device. Works for me.

In HTML,

<div id="hideme">
    <audio id="audioTag" controls>
        <source src="/path/to/audio.mp3">
    </audio>
</div>

with JS

<script type="text/javascript">
window.onload = function() {
    var audioEl = document.getElementById("audioTag");
    audioEl.load();
    audioEl.play();
};
</script>

In CSS,

#hideme {display: none;}
foool
  • 1,462
  • 1
  • 15
  • 29