Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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).

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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?

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    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

  • #7
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Doesn't work either

  • #8
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    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>

  • #9
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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.

  • #10
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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.

  • Users who have thanked fside for this post:

    Pypoli (07-23-2009)

  • #12
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •