Copy text into iframe - JS

260 Views Asked by At

I have this roadblock for some time that I am unable to solve, though it feels like a relatively simple thing to do.

I am trying to pre-fill a form field with a string of text that is on the page. But the form is in an Iframe which is making it difficult.

Here is the page in question however you will not get much out of the link as the dynamic content for you will be empty. This screenshot might explain better: http://prntscr.com/kbqoak

It would grab the text from the "webinartitle" class and insert into form field (http://prntscr.com/kbqqp4).

I fellow user helped me to write up the javascript needed:

$(function(){
$('iframe').contents().find('.refer_webinar input').val($('.webinartitle').text());
});

As instructed I've placed this outside the iframe but the value is still not getting there unfortunately. Here is 2 screenshots that explain the situation: prntscr.com/kbtp4u / prntscr.com/kbtper

Any ideas what I am doing wrong here? Why is it not working?

Thank you in advance for any advice you can give!

1

There are 1 best solutions below

2
On

Unfortunately, it is impossible for a document to communicate with a sandboxed document within itself AKA an iframe's contents.

You can't apply jQuery to other domain iframes. It's blocked due to security reasons.

Check this out for more of an explanation.

https://en.wikipedia.org/wiki/Same-origin_policy