March March - 11 months ago 56
Javascript Question

Can I use javascript to dynamically change a video's source?

How can I change a video's source using JS?

<video id="myVideoTag" width="670" height="377" autoplay="true" controls="controls">
<source src="" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>

Answer Source

Sure, you can just set the src attribute on the source element:

document.querySelector("#myVideoTag > source").src = ""

Or using jQuery instead of standard DOM methods:

$("#myVideoTag > source").attr("src", ""​​​​)​