SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question jQuery selector for YouTube iframe

    Hi there,

    Does anyone know how to select a YouTube iframe with jQuery.

    I am designing a player based on YouTube's js api and my plugin basically selects all of the iframes on a page and transforms then into custom YouTube players. It works ok but isn't suitable for websites that have other iframe content other than YouTube because the plugin will remove them indiscriminately and try to put the player in their place.

    thanks in advance

    Silversurfer
    "Persistence is the path to perfection"

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi silversurfer5150,

    If possible try to add a code which will identified the iframe youtube content and neglect other iframe content.

    Example:

    jQuery('iframe').each(function() {
    var video = $(this);
    var vidSrc = "";
    vidSrc = video.attr('src');
    if(vidSrc.length>29){
    if(vidSrc.substr(0,29)=="http://www.youtube.com/embed/"){

    //Your code

    }
    }
    }
    PHP Programming Its more than just writing code

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jimmy,
    Thanks for your input, that's a good idea, my code actually does something similar already within the plugin but ideally I would like to select the YouTube video element before entering the plugin as in:

    Code:
    $("youtube_movie_selector_here").myplugin();
    I have found various things like $("object").[0] (can't remember exact syntax) and suchlike which can select the child of an object tag but there doesn't appear to be much information about it.

    thanks anyway
    "Persistence is the path to perfection"

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,861
    Mentioned
    206 Post(s)
    Tagged
    12 Thread(s)
    Hi there silversurfer5150,

    You can use the Attribute Starts With Selector

    Code JavaScript:
    var youTubeIframes = $('iframe[src^="http://www.youtube.com/embed/"]');

    HTH

  5. #5
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pullo, that's a new selector to me, great
    "Persistence is the path to perfection"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •