# Is it possible to access html elements contained in an iframe?

**URL:** <https://discourse.processing.org/t/is-it-possible-to-access-html-elements-contained-in-an-iframe/26030>\
**Category:** Coding Questions\
**Created:** [December 8, 2020, 1:35am UTC](https://discourse.processing.org/t/is-it-possible-to-access-html-elements-contained-in-an-iframe/26030 "2020-12-08T01:35:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Bassam](https://discourse.processing.org/u/Bassam)\
**Post date:** [December 8, 2020, 1:35am UTC](https://discourse.processing.org/t/is-it-possible-to-access-html-elements-contained-in-an-iframe/26030/1 "2020-12-08T01:35:50Z")

</div>

I want to access the form data of an html page that is displayed in an iframe from within sketch.js.

Is this possible?

Fullscreen view [https://editor.p5js.org/Bassam/full/mwQs-ReOk](https://editor.p5js.org/Bassam/full/mwQs-ReOk)  
Code [https://editor.p5js.org/Bassam/sketches/mwQs-ReOk](https://editor.p5js.org/Bassam/sketches/mwQs-ReOk)

---

<div class="post-metadata">

**Author:** ![ErraticGenerator](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erraticgenerator/32/11304_2.png) [@ErraticGenerator](https://discourse.processing.org/u/ErraticGenerator)\
**Post date:** [December 8, 2020, 3:10am UTC](https://discourse.processing.org/t/is-it-possible-to-access-html-elements-contained-in-an-iframe/26030/2 "2020-12-08T03:10:04Z")

</div>

You can select an iframe with: `frame.elt.contentWindow.document`  
I added `elt` to select HTML DOM element, not p5js element.

There is an issue with the timing of selecting iframe elements because it may not be ready when you want to use it. I found a neat solution from [https://stackoverflow.com/questions/60461930/checking-if-iframe-contents-are-loaded](https://stackoverflow.com/questions/60461930/checking-if-iframe-contents-are-loaded)

```auto
let frame;
let cnv;
let frameDoc

function setup() {
  frame = createElement("iframe", "")
  frame.attribute("src", "demo.html")
  frame.attribute('id', 'myframe')

  // https://stackoverflow.com/questions/60461930/checking-if-iframe-contents-are-loaded
  frame.elt.addEventListener("load", function() {
    frameDoc = frame.elt.contentWindow.document
    // console.log(frameDoc)

    const p = frameDoc.querySelector('p')
    p.style.color = 'red'
  });
}

```
