SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove extra space inside textarea?

    Hi, does anyone know why i get extra spaces at the start when using textarea??

    I don't want the text indented on the first line of my textarea, i'm using...

    <textarea>
    This is an example text...
    </textarea>

    Any help would be great?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use a bit of javascript to do that.

    suppose your textarea has id="myTextArea"

    if by "extra spaces" you mean leading and trailing white space, then executing the following function will achieve this any time after the page loads (perhaps you want to target some other event however).

    function removeTextAreaWhiteSpace() {
    var myTxtArea = document.getElementById('myTextArea');
    myTxtArea.value = myTxtArea.value.replace(/^\s*|\s*$/g,'');
    }

    the replace function in this case basically says "replace all leading or trailing white space with an empty string"

    hope this helps!

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shark3y3s View Post
    Hi, does anyone know why i get extra spaces at the start when using textarea??

    I don't want the text indented on the first line of my textarea, i'm using...

    <textarea>
    This is an example text...
    </textarea>

    Any help would be great?
    Yes, it's because you have white-space characters between the tags and the text.

    Remove that space if you don't want it there:
    Code HTML4Strict:
    <textarea rows="5" cols="80">This is an example text...</textarea>
    (Note that the rows and cols attributes are required for textarea elements.)
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh, perhaps I'd misinterpreted... thought it was a more general question

    Great answer AutisticCuckoo sounds like that was it as I read it again...

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your post is still valuable, JSM, since it shows how you can trim leading and trailing whitespace in user-supplied content.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

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