user2280642
user2280642

Reputation: 85

Video selection using <select> tag does not work

I am implementing a video selection functionality to my video play so that people can choose which video they would like to watch. My current code goes like this:

html:

<video id="media" width="600" height="400" preload="none">
       <source id="myvideos" src="files/Best of 60s.m4v"> </source>
</video>
<select id="videos">
        <option value="PleaseSelect" selected="selected">Please Select...</option>
        <option value="files/Best of 60s.m4v">Best of 60s</option>
        <option value="files/Black and White Gems.m4v">Black and White Gems</option>
</select>

Javascript:

var selectmenu, mmedia;
selectmenu=document.getElementById('videos'); 
mmedia = document.getElementById('media');

selectmenu.addEventListener('onchange', loadvid);

function loadvid(){
selectmenu.onchange=function(){
    var chosenoption=this.options[this.selectedIndex];
    if (chosenoption.value != "PleaseSelect"){
        mmedia.src = chosenoption.value;
    }
    mmedia.load();  
  }
}

When I run this code no "link" gets created between what I choose in the select menu to what plays except for what I have stated in the src element.

I implemented this with the following mindset:

To have an onchange event attached to the select element so the function loadvid() only fires off when something changes in the menu.

The loadvid function than manages the choice of video by defining the value of the selected index and passing it to mmedia; which is a video element and has the src property.

Once the selection is made the video then should load (mmedia.load()).

I have obviously gone wrong somewhere and would appreciate any guidance you can provide.

Many thanks.

Upvotes: 3

Views: 170

Answers (2)

Digital Chris
Digital Chris

Reputation: 6202

Your javascript has an "onchange" where "change" is needed; in addition extraneous function is commented out below:

var selectmenu, mmedia;
selectmenu = document.getElementById('videos');
mmedia = document.getElementById('media');

selectmenu.addEventListener('change', loadvid);

function loadvid() {
    //selectmenu.onchange=function(){
    var chosenoption = this.options[this.selectedIndex];
    if (chosenoption.value != "PleaseSelect") {
        alert(chosenoption.value);
        mmedia.src = chosenoption.value;
    }
    mmedia.load();
    // }
}

The other problem you're going to encounter is using <source> in your <video> tag; you might have less trouble if you simply use a src= attribute in the video tag:

<video name="media" id="media" width="600" height="400" preload="none" src="files/Best of 60s.m4v"></video>

Upvotes: 1

Lasse
Lasse

Reputation: 1656

You're on the right track, however, there is a "typo" preventing your code from working:

In your addEventListener call, you tell it to listen for onchange which isn't an actual event. MDN has a handy list of events. You'll want to use the change event instead.

The on prefix is used when you add a listener directly as a HTML attribute like so <select onchange="loadvid()">.

Side-note: Since you've already attached an event listener here:

selectmenu.addEventListener('change', loadvid);

...You don't need to do it again here:

function loadvid(){
selectmenu.onchange=function(){

Take care of those 2 and you get this working code:

var selectmenu, mmedia;
selectmenu = document.getElementById('videos'); 
mmedia = document.getElementById('media');

selectmenu.addEventListener('change', loadvid);

function loadvid(){
    var chosenoption=this.options[this.selectedIndex];
    console.log(chosenoption);
    if (chosenoption.value != "PleaseSelect"){
        mmedia.src = chosenoption.value;
    }
    mmedia.load();  
}

Upvotes: 1

Related Questions