Changing Div In Iframe Using Jquery
I have a 'Page A'. In this page is an iframe which displays 'Page B'.
Solution 1:
$("iframe").contents().find("#dTitle").attr('id','cTitle').html($('#cTitle').html());
Solution 2:
Assuming you're not violating the same origin policy,
1, Give your iframe an id - <iframe id="frm" src="javascript:undefined;"></iframe>
2, Get the Window
and HTMLDocument
from the frame
var iWin = document.getElementById('frm').contentWindow,
iDoc = iWin.document;
3, Ceck for existance of jQuery in iframe
if( ! iWin.$ ){
var jq = iDoc.createElement('script');
jq.type = 'text/javascript';
jq.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js';
jq.onload = ready;
iDoc.head.appendChild(jq);
}else{
ready(); // the stuff you want to do
}
4, Do what you want with jQuery
function ready(){
var div = iWin['$']('#frm_div')[0]; // example from fiddle
div.textContent = 'bar';
}
See this fiddle.
Solution 3:
You can change so only if the iframe domain is same as the parent domain. If it is so then you can change it by accessing the iframe from the parent window using the contentWindow property.
Solution 4:
Go and check this post Changing an element's ID with jQuery
$(this).attr("id","newId");
Solution 5:
If you want to change just content then do this:
$("div#bTitle").html($("div#cTitle").html());
Post a Comment for "Changing Div In Iframe Using Jquery"