HTML5 video - show/hide controls programmatically

Weatherman picture Weatherman · Mar 23, 2011 · Viewed 124k times · Source

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.

Answer

jessegavin picture jessegavin · Mar 23, 2011
<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/