How to target only youtube iframe urls?

Go To StackoverFlow.com

4

I'm looking for a way to target this jquery only at src url's that have "youtube.com" in them. I've tried using the :contains selector but I can't get it working correctly.

jQuery(document).ready(function($){

    $('iframe').each(function() {
        var url = $(this).attr("src")
        $(this).attr("src",url+"&wmode=transparent")
    }); 

});
2012-04-04 01:45
by Bryan Casler


15

Use the attribute selector to target only iframes with a src containing youtube.com:

jQuery(document).ready(function($){

    $('iframe[src*="youtube.com"]').each(function() {
        var url = $(this).attr("src")
        $(this).attr("src",url+"&wmode=transparent")
    }); 

});
2012-04-04 01:49
by Aaron
Does the & need to be escaped - Blender 2012-04-04 01:50
No. the valid HTML entity for the ampersand is read by the browser simply as &. There is no need to escape the entity - Aaron 2012-04-04 02:00
I think that came out incorrectly: does the & need to be escaped in the src attribute? I'm pretty sure it doesn't - Blender 2012-04-04 02:24
Worked perfectly, thanks! And as for & being escaped, no it doesn't - Bryan Casler 2012-04-04 02:28