Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding all iframe srcs with jquery

Tags:

jquery

is there a way, with jquery, to for exapmle on click, find all iframe elements, remove their src= tag, and give it back? sort of, refresh :)

Was looking for foreach function or something like that, but I'M rather hopeless at this :(

Thanks for your time, Mart

like image 643
Martin Avatar asked Apr 23 '12 09:04

Martin


4 Answers

$(document).ready(function() {
    $('#somebuttonid').click(function() {
        $("iframe").each(function() {
            var src = $(this).attr('src');
            $(this).attr('src', src);  
        });

    });
});
like image 99
coolguy Avatar answered Nov 19 '22 09:11

coolguy


You can refresh all the iframe like this

$("iframe").each(function() { 
   $(this).attr('src', $(this).attr('src')); 
});
like image 37
Starx Avatar answered Nov 19 '22 09:11

Starx


You can pass a function to .attr() [docs] (or .prop() [docs]):

$('iframe').attr('src', function(index, val) {
    return val;
});

This function is executed for each element. It's a bit more concise than using an explicit .each loop.

like image 35
Felix Kling Avatar answered Nov 19 '22 10:11

Felix Kling


You don't even need jQuery:

for(var i = 0; i < frames.length; i++) {
   frames[i].src = frames[i].src;
}
like image 2
Andreas Wong Avatar answered Nov 19 '22 09:11

Andreas Wong