Adding DIV to iframe body
jquery
Solution
Can you post the full html with iframe? In the example above you have two divs and the code is trying to add to the second div, not an iframe.
This works for me:
<div id="sampleDIV">whatever</div>
<iframe id="custom-frame" src="page2.html" width="500" height="500"></iframe>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
$(function() {
var iframeBody = $("#custom-frame").contents().find("body");
var styleTag = iframeBody.append($('#sampleDIV'));
})
</script>
Problem
Good day, I am trying to add a DIV tag to an iframe body. The content of the iframe is in the same domain of the website I'm working on, so that shouldn't be a problem. ``` <div id="sampleDIV">whatever</div> <iframe id="custom-frame"></iframe> ``` And in my javascript code, I have: ``` // load up the the iframe with the data the user selected $('#custom-frame').attr("src", "/Custom/Data/" + data.id); // I can get find the body tag in the iframe like: var iframeBody = $("#custom-frame").contents().find("body"); var styleTag = iframeBody.append($('#sampleDIV')); ``` * EDITED * But when I view the source, I don't see the div tag added. When I Inspect Element in Chrome, I don't see the DIV element added. * EDITED PART II * I corrected the tag from DIV to IFRAME I can't see what I'm doing wrong, maybe just staring at this too long. TIA, coson