Probably a simple question ?

Discussion in 'Javascript' started by EnjoyNews, Apr 2, 2008.

  1. EnjoyNews

    EnjoyNews Guest


    Probably a simple question.

    How do I write a text in a div while I type it in an input field ?

    I type a text in an input field, and as i type it the text apear in a <div>
    right below.

    I have found this in the net:
    <form name="test" action="">
    <input type="text" size="35" name="text"
    <input type="text" readonly size="35" name="return">

    But this return the intire form instead of just the one input field i need.
    And it also uses another input field to write it in, where I need it to be
    written in a <div>
    EnjoyNews, Apr 2, 2008
    1. Advertisements

  2. EnjoyNews

    jidixuelang Guest

    <div id="blackboard">Please input your message!</div>
    <input type="text"
    onkeyup="document.getElementById('blackboard').innerHTML=this.value;" /
    jidixuelang, Apr 2, 2008
    1. Advertisements

  3. EnjoyNews

    EnjoyNews Guest

    Super :eek:) that was just what I needed

    Is it possible to transfer a line change from a text area also ?

    So when you make a break in the text area it also makes a break in the <div>
    EnjoyNews, Apr 2, 2008
  4. First, a couple of pointers:
    The .innerHTML code above may provide unexpected results if someone uses
    any of HTML's special characters in your text box.

    For instance, try typing
    if a < 10 or a > 20.

    It is slightly more complicated, but you would be better off creating a
    text node and replacing the contents of your 'blackboard.' Google for

    As far as preserving layout:

    There are a couple of ways to do this, depending on context.

    The easiest way would be to more like
    <pre><span id="blackboard"></pre>

    The <pre> element was designed for preformatted text and will preserve
    both new lines and spaces, however it will give a monospaced font.

    I have never tried createTextNode with multiple embedded spaces or new
    lines. I may get called on this, but I believe the results would be

    <URL: >

    is quiet on the subject.
    Jeremy J Starcher, Apr 2, 2008
  5. EnjoyNews

    SAM Guest

    EnjoyNews a écrit :

    onkeyup="var txt = this.value.replace(/[\r\n]/g,'<br>');
    document.getElementById('blackboard').innerHTML= txt;">
    SAM, Apr 2, 2008
  6. EnjoyNews

    EnjoyNews Guest

    cool :eek:)

    Thanks alot
    EnjoyNews, Apr 2, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.