Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2009, 08:31 AM   PM User | #1
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
How to insert text into a text area within an iframe.

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 :
Code:
<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 :
Code:
<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 :
Code:
<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?

Last edited by Pypoli; 07-21-2009 at 11:05 AM..
Pypoli is offline   Reply With Quote
Old 07-21-2009, 12:26 PM   PM User | #2
fside
Regular Coder

 
Join Date: Mar 2008
Posts: 301
Thanks: 2
Thanked 30 Times in 30 Posts
fside is an unknown quantity at this point
Quote:
Originally Posted by Pypoli View Post
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).
fside is offline   Reply With Quote
Old 07-21-2009, 02:05 PM   PM User | #3
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
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.
Pypoli is offline   Reply With Quote
Old 07-21-2009, 02:18 PM   PM User | #4
fside
Regular Coder

 
Join Date: Mar 2008
Posts: 301
Thanks: 2
Thanked 30 Times in 30 Posts
fside is an unknown quantity at this point
Quote:
Originally Posted by Pypoli View Post
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?
fside is offline   Reply With Quote
Old 07-21-2009, 03:50 PM   PM User | #5
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
My code now looks like this :

Code:
<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.

Last edited by Pypoli; 07-21-2009 at 03:53 PM..
Pypoli is offline   Reply With Quote
Old 07-21-2009, 04:08 PM   PM User | #6
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
Try this:
Code:
<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>
__________________
I am still learning English
Amphiluke is offline   Reply With Quote
Old 07-21-2009, 04:55 PM   PM User | #7
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
Doesn't work either
Pypoli is offline   Reply With Quote
Old 07-21-2009, 05:05 PM   PM User | #8
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
try this:
Code:
<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>
ckeyrouz is offline   Reply With Quote
Old 07-21-2009, 06:36 PM   PM User | #9
fside
Regular Coder

 
Join Date: Mar 2008
Posts: 301
Thanks: 2
Thanked 30 Times in 30 Posts
fside is an unknown quantity at this point
Quote:
Originally Posted by Pypoli View Post
My code now looks like this :

Code:
"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:

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

Last edited by fside; 07-21-2009 at 06:38 PM..
fside is offline   Reply With Quote
Old 07-22-2009, 08:45 AM   PM User | #10
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
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.
Pypoli is offline   Reply With Quote
Old 07-22-2009, 04:14 PM   PM User | #11
fside
Regular Coder

 
Join Date: Mar 2008
Posts: 301
Thanks: 2
Thanked 30 Times in 30 Posts
fside is an unknown quantity at this point
Quote:
Originally Posted by Pypoli View Post
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.

Last edited by fside; 07-22-2009 at 04:16 PM..
fside is offline   Reply With Quote
Users who have thanked fside for this post:
Pypoli (07-23-2009)
Old 07-23-2009, 04:25 PM   PM User | #12
Pypoli
New to the CF scene

 
Join Date: Jul 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Pypoli is an unknown quantity at this point
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.

Last edited by Pypoli; 07-23-2009 at 04:46 PM..
Pypoli is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:52 AM.


Advertisement
Log in to turn off these ads.