-3

i have html code

site index.html

<html>
<body>
  <iframe id="html2" frameborder="0" src="html2.htm" marginwidth="0" marginheight="0" style="height: 492px;"></iframe>
</body>
</html> 

site html2.htm

<html>
<body>
  <div><p id='123'>hello</p></div>
</body>
</html>

when i using document.getElementById('123') in site index.html return = null; how i can get it

m59
  • 41,346
  • 14
  • 107
  • 131

1 Answers1

0

You need to first access innerDoc of iframe by:

var iframe = document.getElementById('html2');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

once you get the object, now you can access:

innerDoc.getElementById('divId');

For ID in html, you need to follow these rules:

ID should match:

[A-Za-z][-A-Za-z0-9_:.]*
  1. Must Start with A-Z or a-z characters
  2. May contains _(hyphen), :(colan) and .(dot)

but one should avoid : and . beacause jQuery selector conflict on these with class and filter.

Zaheer Ahmed
  • 26,435
  • 11
  • 70
  • 105