Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2011
    Location
    fort worth, tx
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 11:26 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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.
    Last edited by WolfShade; 02-01-2013 at 11:16 PM.
    ^_^

    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".

  • Users who have thanked WolfShade for this post:

    ten3six (02-01-2013)

  • #3
    New Coder
    Join Date
    Jan 2011
    Location
    fort worth, tx
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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/ )

  • #5
    New Coder
    Join Date
    Jan 2011
    Location
    fort worth, tx
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    .... 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.

  • #7
    New Coder
    Join Date
    Jan 2011
    Location
    fort worth, tx
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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.

  • #9
    New Coder
    Join Date
    Jan 2011
    Location
    fort worth, tx
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Ah. Alright then!


  •  

    Tags for this Thread

    Posting Permissions

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