peggy peggy - 2 months ago 13
HTML Question

Jquery iterate through html elements in iframe

I currently have refactored some code and am getting hung up on some jquery usage. Before, I was using a jquery

each()
loop and iterating through dom elements like this:

$.each($('[id*="cnv"]'), function (index, value) { /*do work*/ }


and this will select each element who's ID contains
cnv
. Now I have moved the code and need to iterate through these elements but from outside the iframe that they live in. I access these elements in various places using javascript as follows

var canvasObj = window.frames['iframe'].contentDocument.getElementById(canvasId);


(note:
iframe
is the id/name of my iframe) but I am not sure how to combine these two statements so that I can iterate through elements with matching ids from a parent.

Answer

You would use contents()

$("#iFrameId").contents().find('[id*="cnv"]').each(...)
Comments