Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 10:01 PM   PM User | #1
ten3six
New Coder

 
Join Date: Jan 2011
Location: fort worth, tx
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
ten3six is an unknown quantity at this point
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?

Last edited by ten3six; 02-01-2013 at 10:26 PM..
ten3six is offline   Reply With Quote
Old 02-01-2013, 10:13 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 02-01-2013 at 10:16 PM..
WolfShade is online now   Reply With Quote
Users who have thanked WolfShade for this post:
ten3six (02-01-2013)
Old 02-01-2013, 10:25 PM   PM User | #3
ten3six
New Coder

 
Join Date: Jan 2011
Location: fort worth, tx
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
ten3six is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
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.
ten3six is offline   Reply With Quote
Old 02-01-2013, 10:28 PM   PM User | #4
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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/ )
Airblader is offline   Reply With Quote
Old 02-01-2013, 10:38 PM   PM User | #5
ten3six
New Coder

 
Join Date: Jan 2011
Location: fort worth, tx
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
ten3six is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
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.
ten3six is offline   Reply With Quote
Old 02-01-2013, 11:04 PM   PM User | #6
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
.... 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.
Airblader is offline   Reply With Quote
Old 02-02-2013, 05:50 AM   PM User | #7
ten3six
New Coder

 
Join Date: Jan 2011
Location: fort worth, tx
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
ten3six is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
.... 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
ten3six is offline   Reply With Quote
Old 02-02-2013, 09:41 AM   PM User | #8
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Old 02-02-2013, 10:05 AM   PM User | #9
ten3six
New Coder

 
Join Date: Jan 2011
Location: fort worth, tx
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
ten3six is an unknown quantity at this point
Quote:
Originally Posted by Airblader View Post
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.
ten3six is offline   Reply With Quote
Old 02-02-2013, 10:25 AM   PM User | #10
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
Ah. Alright then!
Airblader is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, line break, textarea

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:40 PM.


Advertisement
Log in to turn off these ads.