...

View Full Version : How to insert text into a text area within an iframe.



Pypoli
07-21-2009, 08:31 AM
Hi,
I have a page within which is an iframe, and within that iframs is a text area.
I need to be able to insert text into that text area from the mother page.
I have tried to insert it with the following code but it's not working :


<iframe src ="http://iframepage.com" id="myIframe">
<p>Your browser doesn't handle iframes</p>
</iframe>
<input type="button" value="Go" onclick="document.getElementById('myIframe').contentDocument.getElementById('textArea').value = 'test successful';">


Inside the iframe is this code for the text area :


<input class="text" size="50" name="area" id="textArea" value="" maxlength="255" onkeyup="checkconditions(this.value, this.name, this.type)" type="text">


I've also tried to insert it using this script :


<script>
function insertText() {
var textstring = "test successful";
document.getElementById
("myIframe").contentWindow.document.getElementById("textArea").value = textstring;
}
</script>

But nothing works.

Any idea on how i could get this to work?

fside
07-21-2009, 12:26 PM
Hi,
Any idea on how i could get this to work?

You can replace the text simply by using innerHTML:

top.frames["inner1"].window.document.getElementsByTagName("textarea")[0].innerHTML

Assuming "inner1" is the NAME of your iframe (and you're looking just for the first textarea element).

Pypoli
07-21-2009, 02:05 PM
I've tried to use your code with both the javascript link and the button, and it hasn't worked for me.
The textarea is indeed the only one in the iframe so that can't be an issue.

fside
07-21-2009, 02:18 PM
I've tried to use your code with both the javascript link and the button, and it hasn't worked for me.
The textarea is indeed the only one in the iframe so that can't be an issue.

Did you name the iframe - inner1? name='inner1'

I loaded a page into an iframe, with one textarea, and was able to manipulate it from the page with the iframe, as shown, in ie7, ie8, ff3.5, the latest Google and Safari. So, I don't know.

You've got that innerHTML on the left side of the equals sign?

What does your code look like, now?

Pypoli
07-21-2009, 03:50 PM
My code now looks like this :



<script>
function insertText() {
var textstring = "test successful";
top.frames["iframeAB"].window.document.getElementsByTagName("textarea")[0].innerHTML = textstring;
}
</script>

<iframe name="iframeAB" src ="http://soderumo.com/limesurvey/index.php?sid=89767&lang=fr" id="qab" width="100%" height="210" style="border:1px solid #009ee0">
<p>Your browser doesn't handle iframes</p>
</iframe>
<input type="button" value="Go" onclick="top.frames['iframeAB'].window.document.getElementsByTagName('textarea')[0].innerHTML='test successful';">
<a href="javascript:;" onClick="insertText()">test</a>


I've tried various values for the tag name, tried it with getelementsbyid, getelementsbyname etc and nothing seems to work.

Thanks for your help.

Amphiluke
07-21-2009, 04:08 PM
Try this:

<script type="text/javascript">
function insertTxt() {
var textstring = "test successful";
window.frames["iframeAB"].document.documentElement.getElementsByTagName("textarea")[0].value = textstring;
}
</script>

<iframe name="iframeAB" src ="iframe.htm" id="qab" width="100%" height="210" style="border:1px solid #009ee0">
Your browser doesn't handle iframes
</iframe>
<input type="button" value="Go" onclick="window.frames['iframeAB'].document.documentElement.getElementsByTagName('textarea')[0].value='test successful';"/>
<a href="#" onclick="insertTxt();">test</a>

Pypoli
07-21-2009, 04:55 PM
Doesn't work either :(

ckeyrouz
07-21-2009, 05:05 PM
try this:

<script type="text/javascript">
function insertTxt() {
var textstring = "test successful";
parent.iframeAB.document.getElementsByTagName("textarea")[0].value = textstring;
}
</script>

<iframe name="iframeAB" src ="iframe.htm" id="qab" width="100%" height="210" style="border:1px solid #009ee0">
Your browser doesn't handle iframes
</iframe>
<input type="button" value="Go" onclick="insertTxt()"/>
<a href="#" onclick="insertTxt();">test</a>

fside
07-21-2009, 06:36 PM
My code now looks like this :



"http://soderumo.com/limesurvey/index.php?sid=89767&lang=fr"



When you dribble this stuff out, it's not helpful. You might have mentioned this URL in the OP.

You don't have ANY textareas. That's why you can't locate them. There ain't none!

What you have is a bunch of input elements. The sixth one is the one you want - better to use an id attribute. But for the time being:



top.frames['iframeAB'].window.document.body.getElementsByTagName('input')[5].value='test successful'


Gotta work.

Pypoli
07-22-2009, 08:45 AM
Thanks for the helpful post, I feel like a moron for the textarea thing btw, but that's what you get when you try to code with 3 hours sleep ><
However, it's still not working. Recounted, tried every number from 0 to 15 in case i was missing something, tried to revert to grabbing it through id, but nothing goes.

fside
07-22-2009, 04:14 PM
However, it's still not working.

Worked in every major browser.

Here's the thing. Javascript locks out - cross-site - scripting. These have to be YOUR pages, on your server.

They are, right? I tested by downloading the page, and using that, instead. I have scripts of my own, on my own sites, that do a great deal of modification to pages loaded into iframes. But they are all pages on my server.

Pypoli
07-23-2009, 04:25 PM
That would explain the issue. They are "my" pages (per se, i'm doing this as part of my job for a company), but they're on 2 different servers. The idea being to imbed a page that uses PHP in a page from a server that doesn't.

At least i've got that cleared up and i can stop banging my head against the wall now. Thanks a lot for your help.

Edit : I've checked with the page on the same server, and obviously it works. I need to find some other way to do what i want.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum