Dynamically Load YouTube Video Using jQuery

This Tutorials helps you to add dynamically youtube video in iframe.  To add dynamically load the YouTube Video, you’ve to change the iframe src attribute using the jQuery. Following is the demo and download the source.

Demo | Download


Create a file called index.html (or your own project).


<!DOCTYPE html>
<title>Load YouTube Video using jQuery</title>
<meta charset=utf-8 />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="script.js"></script>
  <label for="videoUrl"><strong>Youtube Video ID</strong></label>
  <input type="text" id="videoUrl" name="videoUrl" value="E6KwXYmMiak">
  <div id="mydiv"><iframe id="frame" src="" width="500" height="300">
  <button id="button">Load</button>

Now, create a file called script.js and paste the following code.


  $("#button").click(function () { 
      $("#frame").attr("src", "http://www.youtube.com/embed/"+ $("input#videoUrl").val() +"?rel=0&autoplay=1");



Please follow and like us:

Leave a Comment

Your email address will not be published. Required fields are marked *