T
T
Timur Sharipov2015-11-20 09:08:42
JavaScript
Timur Sharipov, 2015-11-20 09:08:42

How to reach with jQuery to an element inside a frame?

treeview.html frame file:

<!DOCTYPE HTML >
<html>
<head>
<title></title> 
</head> 

<body>     
<div id="tree">123</div>
</body>
</html>


In the Index.html file, I'm trying to get text from a div:
<!DOCTYPE HTML >
<html>
<head> 
<script type="text/javascript" src="src/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">           
    $(function main() {
    //alert(1);
    alert($("#treeview").contents().find("#tree").text());
    });
</script>
</head> 

<iframe src="treeview.html" id="treeview" name="treeview">
</iframe>
</html>


Result:
Firefox 42.0: gives an empty field
IE9: "123" as it should
Chrome: empty field

If you ask alert(1) first, then
Firefox 42.0: "123"
as it should IE9: " 123" as it should
Chrome: empty field

?? ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ernest Faizullin, 2015-11-20
@shtr

var iframe = document.querySelector('#treeview');
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
var tree = iframeDocument.getElementById('tree');

UPD: jsfiddle.net/erniesto77/2s485/108

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question