CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Resolved line breaks not working in textarea (http://www.codingforums.com/showthread.php?t=286897)

ten3six 02-01-2013 10:01 PM

line breaks not working in textarea
 
I have searched and found no solution so...
I've got a textbox that I am changing to a textarea so that I can show multiple lines but my javascript is not showing the line breaks. I know that \n should work but the display seems to be just ignoring it, not printing it. Does anyone know what I'm doing wrong?

Here is my textarea
Code:

<table class="COPY">
        <tr>
                <td class="COPY">
                        <!--input type="text" value="" name="COPYINPUT" class="COPYINPUT" ID="COPYINPUT"-->
                        <input type="textarea" value="" name="COPYINPUT" class="COPYINPUT" ID="COPYINPUT">
                </td>
        </tr>
</table>

and here is the CSS for it...
Code:

.COPY
        {
                border:0px inset #F5F5F5;
                background-color:#F5F5F5;
                border-color: #F5F5F5;
                color: #F5F5F5;
        }
.COPYINPUT
        {
                border:2px inset #F5F5F5;
                background-color:#F5F5F5;
                border-color: #F5F5F5;
                /*color: #F5F5F5;*/
                height: 300px;
                width: 200px;
                text-align: left;
                vertical-align: top;
                overflow: visible;
        }

and HERE is my javascript that isn't working
Code:

document.Finance.COPYINPUT.value=("First Line\nSecond Line\\nThird Line\n\nFourth Line\n\rFifth Line\r\nSixth Line<br>Seventh Line")
and this is what shows up in the textarea

First LineSecond Line\nThird LineFourth LineFifth LineSixth Line<br>Seventh Line

As you can see "\n" is treated like it's not even there. Nothing seems to work. What am I doing wrong?

WolfShade 02-01-2013 10:13 PM

It's not <input type="textarea">,

it's <textarea name="" id=""></textarea>

And \n is only for JavaScript, not textarea or input type="text". If you just hit ENTER, the line breaks will be there (if you are typing in the textarea); if you're trying to SET the value of the textarea with line breaks, I think (I've never tried this, before) that you use the ANSI code for those.

ten3six 02-01-2013 10:25 PM

Quote:

Originally Posted by WolfShade (Post 1310445)
It's not <input type="textarea">,

it's <textarea name="" id=""></textarea>

And \n is only for JavaScript, not textarea or input type="text". If you just hit ENTER, the line breaks will be there (if you are typing in the textarea); if you're trying to SET the value of the textarea with line breaks, I think (I've never tried this, before) that you use the ANSI code for those.

You, my friend, are a genius. Thank you.:thumbsup:

Airblader 02-01-2013 10:28 PM

Actually you can use \r\n (both needed for Windows). But why are you surprised it shows up as '\n' if you escape it to '\\n'?

Code:

document.getElementById('myTextarea').value = 'Hi!\r\nThis is a new line';
That works just fine ( http://jsfiddle.net/N6fNB/ )

ten3six 02-01-2013 10:38 PM

Quote:

Originally Posted by Airblader (Post 1310447)
Actually you can use \r\n (both needed for Windows). But why are you surprised it shows up as '\n' if you escape it to '\\n'?

Code:

document.getElementById('myTextarea').value = 'Hi!\r\nThis is a new line';
That works just fine ( http://jsfiddle.net/N6fNB/ )

I wasn't surprised. I knew that would happen. I also wasn't surprised that "<br>" showed up but in all of my searching for a solution those were more often then not suggested. I just wanted to show that it DIDN'T work to avoid getting those answers.

Airblader 02-01-2013 11:04 PM

.... But it didn't occur to you to try it without escaping? Because it does work as my fiddle shows. Or am I not getting something? I feel like I must be misunderstanding you.

ten3six 02-02-2013 05:50 AM

Quote:

Originally Posted by Airblader (Post 1310458)
.... But it didn't occur to you to try it without escaping? Because it does work as my fiddle shows. Or am I not getting something? I feel like I must be misunderstanding you.

I did try it without escaping. Look at my code...

document.Finance.COPYINPUT.value=("First Line\nSecond Line\\nThird Line\n\nFourth Line\n\rFifth Line\r\nSixth Line<br>Seventh Line")

It wasn't working

Airblader 02-02-2013 09:41 AM

Gotcha now – I didn't read that line carefully enough. Sorry. Does my jsFiddle work for you, though? Because it works for me both on Ubuntu and Windows.

ten3six 02-02-2013 10:05 AM

Quote:

Originally Posted by Airblader (Post 1310500)
Gotcha now – I didn't read that line carefully enough. Sorry. Does my jsFiddle work for you, though? Because it works for me both on Ubuntu and Windows.

It does. The problem was with the way I was setting up the textarea. Once that was fixed everything works as it should.

Airblader 02-02-2013 10:25 AM

Ah. Alright then!


All times are GMT +1. The time now is 08:20 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.