www.webdeveloper.com
Results 1 to 3 of 3

Thread: Why doesnt this work

  1. #1
    Join Date
    Apr 2012
    Posts
    3

    Question Why doesnt this work

    <!Doctype html>
    <html lang="en-us">
    <head>
    </head>
    <body>
    <textarea id="code" style="height:80%;width:50%;position:absolute;left:0%;bottom:0%;"onkeyup="repeat()"></textarea>

    <script type="text/javascript">
    function repeat()
    {
    var code="document.getElementById("code").value;
    document.write('<frame id="edited" style="height:80%;width:50%;position:absolute;bottom:0%;right:0%;>'+code+'</frame>');

    }
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Apr 2012
    Posts
    55
    There's a few things wrong with it. There are some problems with the quotation marks. I think you meant to use an iframe instead of a frame tag. But most importantly, document.write doesn't really work the way you expect after the document loads.

    I think this example does what you wanted:

    PHP Code:
    <!Doctype html>
    <
    html lang="en-us">
    <
    head>
    </
    head>
    <
    body>
    <
    textarea id="code" style="height:80%;width:50%;position:absolute;left:0%;bottom:0%;" onkeyup="repeat()"></textarea>
    <
    iframe id="edited" style="height:80%;width:50%;position:absolute;bottom:0%;right:0%;"></iframe>
    <
    script type="text/javascript">
    function 
    repeat()
    {
    var 
    code document.getElementById('code').value;
    var 
    edited document.getElementById('edited');
    var 
    doc edited.contentDocument || edited.contentWindow.document;
    doc.body.innerHTML code;
    }
    </script>
    </body>
    </html> 

  3. #3
    Join Date
    Mar 2011
    Posts
    1,110
    There are at least two reasons why your code doesn't work. First, when you call document.write() after the page has loaded, it (usually) erases the document and starts over from scratch. You should use document.getElementById(??).innerHTML to manipulate the content of an element on your page. Second, you've misused the <frame> tag. Use <div> instead. And check the 'style' attribute for a missing last quotation mark.
    Rick Trethewey
    Rainbo Design

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles