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 3 of 3

Thread: String Question

  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    182
    Thanks
    139
    Thanked 0 Times in 0 Posts

    String Question

    Hello,

    I need your help.

    For one reason or another, I have a textarea with text in it, however, when I call it back and store it in var, it seems to strip all the spaces.

    [ID = "COMMENTS"]
    Ie. of what's in the textarea:
    ----------------------------------------------
    Apples are red

    Oranges are orange

    Pears are green

    Bananas are yellow


    var comments = document.getElementById("comments").value

    returns:
    Apples are red Oranges are orange Pears are green Bananas are yellow

    How could this be re-written such that the spaces in between the text aren't ignored?

    Much thanks and appreciation for everyones help.

    Cheers

    J

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    What do you mean by spaces are being ignored. Do you mean the line break?

    Are you sure you have hit the return key from a line to another or the textarea width is small so when you are typing the whole sentence it is putting it on several lines?


    I have done a small example here and it seems to be working fine?

    Can you give me more details about your problem and how to reproduce?

    Would be much better if you posted your code here.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <body>
    <textarea id="ta">
    hel   lo?
    
    whasssup?
    </textarea>
    <div id=dsply></div>
    </body>
    <script type="text/javascript">
    var taValue = document.getElementById("ta").value
    
    document.getElementById("dsply").innerHTML = 
    taValue.split(" ").join("&nbsp;").split("\n").join("<br>");
    
    </script>


  •  

    Posting Permissions

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