How do you change video src using jQuery?

Aximili picture Aximili · Oct 21, 2010 · Viewed 110.8k times · Source

How do you change the src of a HTML5 video tag using jQuery?

I got this HTML:

<div id="divVideo">
  <video controls>
    <source src="test1.mp4" type="video/mp4" />
  </video>
</div>

This doesn't work:

var videoFile = 'test2.mp4';
$('#divVideo video source').attr('src', videoFile);

It changes the src if I inspect it using firebug, but does not actually change the video being played.

I read about .pause() and .load(), but I'm not sure how to use them.

Answer

Šime Vidas picture Šime Vidas · Oct 21, 2010

Try $("#divVideo video")[0].load(); after you changed the src attribute.