brandozz
brandozz

Reputation: 1119

Creating an iframe with jquery

Is it possible to create a youtube iframe with jquery. I'd like to load the iframe on click instead of on load. Similar functionality to the lazyYT plugin.

$('.video-container').html(function () {
        var youTubeIF = '<iframe width="1280" height="720" src="//www.youtube.com/embed/myvideoid?rel=0" frameborder="0" allowfullscreen=""></iframe>';
        return youTubeIF;
    });

Upvotes: 0

Views: 26

Answers (1)

BahaEddine Ayadi
BahaEddine Ayadi

Reputation: 987

You can use this code :

$('#click').on('click',function(){
    var code="<iframe width='1280' height='720' src='//www.youtube.com/embed/myvideoid?rel=0' frameborder='0' allowfullscreen=''></iframe>";
    $('.container').append(code);
});

And this is the HTML for it:

<button id="click">Click here</button>
<div class="container"></div>

Here's the fiddle for it:

Upvotes: 1

Related Questions