Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the <img /> in document from iframe?

enter image description hereI have an iframe

<iframe src="../Images/landingpage.jpg" id="Iframe1" name="ifrmContent" class="ifrmClass">
</iframe>

I found in inspector element that img tag lies in "#document" which has body tag

<html>
    <body style="margin: 0px;">
        <img style="-webkit-user-select: none;" src="../Images/landingpage.jpg">
    </body>
</html>

I need to access this img("landingpage.jpg") in order to change(image is small.. need to resize) its width to 100% and height:90%

I have tried using #Iframe1>#document>html>body>img{width:100%;}

like image 972
Joseph Avatar asked Apr 09 '14 04:04

Joseph


4 Answers

Try

$("#Iframe1").contents().find("img").css({
    'width': '100%',
    'height': '90%'
});

.css()

.find()

.contents()

like image 121
Tushar Gupta - curioustushar Avatar answered Oct 22 '22 23:10

Tushar Gupta - curioustushar


You can do this:

$('#Iframe1').contents().find('img').css({'width':'100%','height':'90%'});

.contents() will provide you the contents of the iframe and .find() finds you the image and the .css() is used to apply the style to the found image.

like image 40
Amit Joki Avatar answered Oct 22 '22 22:10

Amit Joki


Non-jQuery option

This question is very similar to this one

Based on the answered question on the link above, you can try that:

var image = window.frames['Iframe1'].document.getElementByTagName('img')[0];
image.styles.width = '100%';
image.styles.height = '90%';
like image 5
roshiro Avatar answered Oct 22 '22 23:10

roshiro


You are using an iframe to load an image. The reason there is a document->html->body->img is because your browser is formatting the request for an image into proper HTML.

This may be obvious (and not what you're after), but for this particular request, you should use the img tag:

<img src="../Images/landingpage.jpg" id="img1" alt="Image 1">

You can then easily change the width and height, as you would any other element in your site.

like image 2
jackfrankland Avatar answered Oct 22 '22 22:10

jackfrankland