1

I am attempting to display text while it is being written within a textarea inside an iframe. So it will be displayed as though through an external page.

The current code displays the input in the div with id "div". But I want to display it in an iframe instead, is that possible?

My current code is:

<script language='javascript'>
  function hot(){
    document.getElementById("div").innerHTML=document.getElementById("text").value
  }
  window.onload = function() {hot();};
</script>
<textarea id="text" onKeyUp="hot();"></textarea>
<div id="div"></div>
2
  • So, what is it that you're having trouble with? Commented Jul 21, 2013 at 15:20
  • The problem is I want the input to display in an iframe and not a div. Thanks! Commented Jul 21, 2013 at 17:26

2 Answers 2

1
<script type='text/javascript'>
  function hot(){
   window.frames['iframe'].document.body.innerHTML=document.getElementById("text").value
  }
</script>
<textarea id="text" onKeyUp="hot();"></textarea>
<iframe id = "iframe"></iframe>

http://jsfiddle.net/dD3R3/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I picked it as the accepted answer just because it is simpler than the others :)
0

Based on the answers to this question, the following code should do the trick:

<textarea id="text">Type &lt;b&gt;HTML&lt;/b&gt; here...</textarea><br>
<iframe id="frame">
$( function () {
    var $text = $( '#text' );
    var doc   = $( '#frame' )[0].contentWindow.document;
    var $body = $( 'body', doc );
    var hot = function () {
        $body.html( $text.val() );
    };
    $text.keyup( hot );
    hot();
} );

Here's a live demo of the code above on jsFiddle.

(I took the liberty of using jQuery, since you tagged your question with it, but it shouldn't be hard to translate this code back to pure JavaScript if that's what you prefer.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.