I'm new to js and have only been learning for about a week so be kind. The script below alters the src of an iframe to match the custom data-attribute of the same iframe when a link with a class of .thumb is clicked.
This works fine but its currently changing the iframe src of all classes of all iframes with a parent div class of .gallery. I just want to change the iframe src of the .gallery with the same index value as the variable 'clicked'.
//When an link with a class of .thumb is clicked
$(document).on('click', '.thumb', function () {
//variable index is the index of the links parent 'li'
var clicked = $(this).parent('li').index();
// Find iFrames of all .comm-gallery elements and alter their src to iframes data-src value
$('.gallery').find("iframe").prop("src", function(){
// Set their src attribute to the value of data-src
return $(this).data("src");
});
});
I've tried numerous loop solutions over the past few hours but none have worked and, as I know little about js at this point, I imagine that I am the lowest common denominator in this. Any help would be greatly appreciated!
.eqlike$('.gallery').find("iframe").eq(clicked).prop(..?.eqworks for you, then I suggest you go by that. It is more like iterating 1 item vs iteration all item and comparing the index.