I’m trying to select the closest iframe with jQuery. There are a lot of these windows so, I’d like to select the nearest iframe. I’d expect closest() to work, but no luck.
JavaScript:
$(document).ready(function(){
var d = $(document);
d.on('click', 'a.load_url', function() {
var myFrame = $(this).closest("iframe").attr("class");
alert("myFrame: " + myFrame);
});
});
HTML:
<div id="window2">
<div class="navbar_top">
<span class="float_left">
<a href="#" class="load_url">Click Here</a>
</span>
</div>
<div class="window_content" style="border: solid 2px blue; overflow:hidden;">
<iframe class="classy_iframe" src="http://mx7racing.com"> </iframe>
</div>
</div>
.closesttraverses up the DOM (parents) and finds the first that matches.To find the
iFrameyou’ll need to do something like this: