video.js - update video source when clicking on a link

user2397591 Source

I am trying to have a page with embedded video that dynamically will change the source when a link below the video frame is clicked. The source videos are on my host server. i.e. this pic illustrates it:

![sample of page][1]

I came across this page, which seems to have the answer, but I tried it and it didn't work. Following the example, I pasted the css & javascript in the and the necessary html in the body. I updated all the references to my urls and tried to keep file names the same as the example for testing. Below is what I tried.

Can someone point out my errors, or provide a more elegant way of doing this? Basically dynamically change embedded video when link is clicked and the video work in all the typical browsers, and most devices. This is for a wordpress site, using JW Player for wordpress, (my error) instead I found this script/code is actually for Video.js

It loads the pre image but doesn't play.

As a test I tried this and it does play single video properly:

<video id="testvideo" class="video-js vjs-default-skin" width="440" height="300"     controls="controls">
              <source src="" type="video/mp4"/>
              <source src="" type="video/webm"/>
              <source src="" type="video/ogg"/>

The javascript version for multiple source links

        <style media="screen" type="text/css">
.wrap            { margin:30px auto 10px; text-align:center }
.container       { width:440px; height:300px; border:5px solid #ccc }
p                { font: 10px/1.0em 'Helvetica',sans-serif; margin:20px }
$("input[type=button]").click(function() {
    var $target         = "testvid_"+$(this).attr("rel");
    var $content_path   = "";  
    var $vid_obj        = _V_("div_video");

    // hide the current loaded poster

    $vid_obj.ready(function() {
      // hide the video UI
      // and stop it from playing
      // assign the targeted videos to the source nodes
      // replace the poster source
      // reset the UI states
      // load the new sources

</script>   </head>

        <section class="container wrap">
  <video id="div_video" class="video-js vjs-default-skin" controls preload="auto" width="440" height="300" poster="" data-

    <source src=""  type="video/mp4">
    <source src=""  type="video/ogg">
    <source src=""  type="video/webm">

<div class="wrap">
  <input rel="01" type="button" value="load video 1">
  <input rel="02" type="button" value="load video 2">
  <input rel="03" type="button" value="load video 3">


The preload image for the 1st video loads but no video, error is "No video with supported format and MIME type found"

So I added the source for the first video in this section

        <source src=""  type="video/mp4">
        <source src=""  type="video/ogg">
        <source src="  type="video/webm">

Result the 1st video loads but not the other linked videos.

names of the videos/png: testvid_01.mp4, testvid_01.ogv, testvid_01.webm, testvid_01.png testvid_02.mp4, testvid_02.ogv, testvid_02.webm, testvid_02.png testvid_03.mp4, testvid_03.ogv, testvid_03.webm, testvid_03.png

I have tried this both in wordpress page and html page the results are the same.

I'm not sure even if this script will do what I want?



answered 5 years ago heff #1

The javascript example doesn't appear to include the video.js library. You might try including the following in the head.

<link href="" rel="stylesheet">
<script src=""></script>

Otherwise, is there a way to view the page live somewhere?

answered 5 years ago misterben #2

This overwrites the video element's src attribute three times, so it will always be set to the webm video.


Instead use the video.js API to load an array of sources so video.js can pick one the current playback tech can play:

  { type: "video/mp4", src: $content_path+$target+".mp4" },
  { type: "video/webm", src: $content_path+$target+".webm" },
  { type: "video/ogg", src: $content_path+$target+".ogv" }

Updated fiddle:

answered 4 years ago kenjikato #3

Adding to the overall answer to the original question, the video switching answered by misterben above still works in the current version (4.8.0) of video.js, but the code that changes the poster image does not work from the original question post, or in the jsfiddle code sample as of 9/5/2014.

You need to make two small modifications to user239759's original question code (referenced below) from:

// hide the current loaded poster


// replace the poster source

to this:

// hide the current loaded poster


// replace the poster source

This is if you are using the standard video.js code, and associated css.

It should also be noted for those that haven't figured it out from the code above, the poster images should be named the same as the videos except for the file extension, and placed in the same directory as the videos. Unless you use a different var for $content_path for the poster images.

comments powered by Disqus