Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery changing contents of an iFrame

Tags:

jquery

iframe

I'm completely new to using jQuery. Why isn't this working?

<iframe width="800" scrolling="no" id="prev" src="">your browser needs to be updated. </iframe> <script src="jquery.js"></script> <script>     //var c = 0;     $(document).ready(function() {         $('#prev').contents().html("<html><body><div> blah </div></body></html>");     }) </script> 

Also, I am planning on using this iFrame to present a preview of changes to an html file to a user. I am going to either empty the entire iFrame every time a change is made or, if I can figure out how to do so, change particular lines using jQuery. Is that the best way to go about this?

like image 303
mavix Avatar asked Feb 28 '12 22:02

mavix


People also ask

How to change iframe content using jQuery?

If you want to update the iframe content using html then you should first find the body element inside the iframe and then append the required markup.

Can you use jQuery in an iframe?

However, it's also possible to use a really simple jQuery to access the elements within the iFrame. Check it out below: $(document). ready(function(){ var iFrameDOM = $("iframe#frameID").


1 Answers

If you want to update the iframe content using html then you should first find the body element inside the iframe and then append the required markup.

$(document).ready(function() {     $('#prev').contents().find('body').html('<div> blah </div>'); }); 

Working demo - http://jsfiddle.net/ShankarSangoli/a7r9L/

like image 85
ShankarSangoli Avatar answered Sep 19 '22 07:09

ShankarSangoli