Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML HTML Video and Audio (Retired) Captioning Video The Track Element

Hi, How can I make the video start&stop by clicking anywhere on the video icon instead of targeting the play button?

Trying to make the video controls more responsive and user friendly.

Also, how I can make the space bar start and stop the video play?


4 Answers

with this javascript code:

var video = document.getElementById('video-element-id-here');

document.onkeypress = function(e) { if ( (e || window.event).keyCode === 32 /* space key */ ) { video.paused ? video.play() : video.pause(); } };

Nicholas Woods
Nicholas Woods
6,260 Points

Can someone explain what parameter is being passed for 'e'. Also where there is the comment stating /* space key */ is something supposed to go here? Sorry, I'm still very new to this and tried this code and cannot get it to work.

Cool beans. It worked. Thanks.

plus to make play/stop responsive with the click anywhere on the icon. add the script towards the bottom of the html page.

video.onclick = function(e) { video.paused ? video.play() : video.pause(); };


Hi kabir, I suppose I was referring to the entire video player at the time. I remember googling the above code to make that option available only to discover that it was covered in the next video.