How do I edit the output of a textarea form using JavaScript?

Discussion in 'JavaScript' started by dementic, Jan 14, 2008.

  1. #1
    I have to create a form for work. It involves some JavaScript. I have to use a textarea field, but the problem is that I need the output to look EXACTLY like the Input that someone would type into the textarea field.

    This is what I am talking about:
    <html>
    <head>
    <title>Textarea problem</title>
    <SCRIPT language= "javascript">

    function DispMsg(text1)
    {
    document.write("The text you submitted is: <br>" +text1)
    document.textform.reset()
    }

    </SCRIPT>
    </head>

    <BODY>
    <FORM NAME=textform>
    <h2><b>Input Textarea Form</b></h2>
    <br>
    <TEXTAREA NAME="text1" rows="10" cols="40" TYPE="text">COPY/PASTE HERE</TEXTAREA>

    <INPUT NAME="submit" TYPE=Button VALUE="Submit"
    OnClick="DispMsg(form.text1.value)">

    </FORM>
    </BODY>
    </html>


    So, if you type into the textarea field:

    The dog is brown.
    The dog is brown.
    The dog is brown.
    The dog is brown.

    It will output into a single line:
    The dog is brown. The dog is brown. etc..

    How do I get it to output into 4 separate lines as above?
     
    dementic, Jan 14, 2008 IP
  2. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Try this one..

    
    <html>
    <head>
    <title>Textarea problem</title>
    <SCRIPT language= "javascript">
    
    function DispMsg(text1)
    {
    document.write("The text you submitted is: <br>" + text1.replace(/\n/g, "<br/>"));
    document.textform.reset();
    }
    
    </SCRIPT>
    </head>
    
    <BODY>
    <FORM NAME=textform>
    <h2><b>Input Textarea Form</b></h2>
    <br>
    <TEXTAREA NAME="text1" rows="10" cols="40" TYPE="text">COPY/PASTE HERE</TEXTAREA>
    
    <INPUT NAME="submit" TYPE=Button VALUE="Submit"
    OnClick="DispMsg(form.text1.value)">
    
    </FORM>
    </BODY>
    </html>
    
    HTML:
    I replaced the first line of your function "DispMsg" from...

    document.write("The text you submitted is: <br>" + text1);
    Code (markup):
    to...

    document.write("The text you submitted is: <br>" + text1.replace(/\n/g, "<br/>"));
    Code (markup):
    Hope that helps.. :)
     
    rkquest, Jan 14, 2008 IP
  3. dementic

    dementic Peon

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your help!
     
    dementic, Jan 15, 2008 IP