Search code examples
javascripthtmlprototypejstextarea

remove initial value " " of a textarea


I would like to remove the initial space in a textarea. When there is no text given to a textarea and you get the textarea.value, you get a single space " " instead of the empty string like a normal input textbox. Also when there is text (i.e. test) it returns " test". Is it possible to prevent this extra space? I would prefer to work with the prototype of Textarea because I am using a general procedure to get the values of both input textboxes and textarea's.

For the moment I wrote something like this:

Textarea.prototype.value = function() {
        var val = this.value;
        if (val[0] == "0") val.splice(0,1);
        return val;
    };   

But it doesn't solve the problem, I still get the extra space.


Solution

  • Your HTML should be like this:

    <textarea></textarea>
    ---------^ no space, tab or new line there