1

Any function to target that same iframe object is not working and the following error is thrown:

Uncaught TypeError: target[0].getElementsById is not a function

I want to access the value of objects of an iframe

function fetchmap(target) {
  console.log(target[0])
  var el = target[0].getElementsByTagName('input #pac-input');
  var e = target[0].getElementsById('pac-input')
  console.log(el);

};
<div class="col-md-12 px-4 md-form form-group" id="placefinder">
 <iframe id="iframe" src="//developers.google.com/my-business/content/tools/placeid-lookup" width="100%" height="400px" onload="fetchmap($(this))"></iframe>
</div>
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Possible duplicate of [Invoking JavaScript code in an iframe from the parent page](https://stackoverflow.com/questions/251420/invoking-javascript-code-in-an-iframe-from-the-parent-page) – VLAZ May 13 '18 at 11:20

1 Answers1

0

It's getElementById (singular), not getElementsById (plural). It returns a single element.

Also note that the elements in a document inside the iframe are not descendants of the iframe itself. You have to use iframe.contentDocument.getElementById().

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
  • thanks, but I have this problem : Uncaught DOMException: Blocked a frame with origin "http://127.0.0.1:8000" from accessing a cross-origin frame. and ifram src is https://developers.google.com/my-business/content/tools/placeid-lookup – mina fathollahi May 14 '18 at 06:46
  • Well, that's a different matter. Scripts aren't allowed to access the DOM structure of windows and iframes that are loaded from different domains (to prevent malicious code from hijacking your browser). – Máté Safranka May 14 '18 at 08:28