Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

HTML5 Video to play when button clicked

Featured Replies

On my site I have a small video playing in a loop in the top left corner, I want to click on a button and replace the video with another, then when the video has played I want to go back to the first video. There will be 7 buttons to click playing 7 videos, one at a time of cause.


I'm not that savvy with Javascript to do this, I would appreciate any help on how to do this with HTML5 video.


Thanks.

You will have to use JS. You need to use two events

1. an onclick event that triggers a function which changes the src attribute of the new video

2. use the ended event that the videoplayer returns when finished to trigger another function which resets the src attribute to the first video.

http://www.w3schools.com/tags/ref_av_dom.asp

 

EDIT: Since you have 7 buttons it might be a good idea to create the buttons as links and then target their href in your onclick function something like this: (not tested)

event.preventDefault(); //prevent the browser to actually follow the links
var myVideo = document.getElementById("your_video:players_id");
myVideo.setAttribute('src', this.href); 
myVideo.play();

Edited by Nillervision

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.