jQuery Replace YouTube Links with Flash Video

By Sam Deering


jQuery code snippet to replace all links with embed flash video on web page or blog post/page.


Go to, find the video (for example, copy link and paste it in your page/blog post. Change in script width and height to your desired parameters and change ‘#blog .post’ to your page content container or blog post container.

    var vidWidth  = 459; // youtbe video width
    var vidHeight = 283; // youtube video height
    var obj       = '' +
        '' +
        '' +

    $('#blog .post').find('p:contains("")').each(function(){
        var that = $(this);
        var vid = that.html().match(/(?:v=)([w-]+)/g);
        if (vid.length) {
            $.each(vid, function(i){
                that.replaceWith( obj.replace(/[vid]/g, this.replace('v=','')) );

See live demo

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.