Why is textarea filled with mysterious white spaces?
Asked Answered
P

23

379

I have a simple text area in a form like this:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink">
<?php     if($siteLink_val) echo $siteLink_val;?> 
                            </textarea>

I keep getting extra white space in this textarea. When I tab into it my cursor is like in the middle of the textarea and not in the beginning? What is the explanation?

Purveyor answered 4/2, 2010 at 20:35 Comment(2)
As firmly defined in all upvoted answers, tldr; is this behavior is in the desktop client browser window. It depends how the DOM interpreter handles it but any excess spaces are whitespaces/comments - but they are still is included in HTML renderer. Thus the newlines/whitespaces, combine <?php and ?> with no whitespace to the next DOM element defined (<textarea><?php ... ?></textarea>)Selfdeception
Do not insert any spaces or newlines inside the <textarea></textarea> in the code editor. <textarea id="mytxtarea" name="mytxtarea">@Model.ContactName</textarea> If you insert spaces or enters inside, like the next sample pseudocode, you will get misterious spaces inside your textarea in the web page: <textarea>enter space mytextInside space enter space </textarea>Stevens
S
641

Look closely at your code. In it, there are already three line breaks, and a ton of white space before </textarea>. Remove those first so that there are no line breaks in between the tags any more. It might already do the trick.

Note that if($siteLink_val) condition is superfluous in this case and can be safely removed.

What is more important, all HTML output must be obligatory HTML-encoded. Hence your code could be like this

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php 
    echo htmlspecialchars($siteLink_val);
?></textarea>
Surbase answered 4/2, 2010 at 20:37 Comment(20)
@user79685 you're welcome. Read my new comment above, I wasn't really ridiculing you. At least not in a mean way :)Surbase
Mmm, I don't agree with that. I'm very much for tact and etiquette in online discussions, and I enjoy the overall very friendly tone on SO. On the other hand, one needs to develop a bit of a skin when moving about on the net, that's true.Surbase
very nice. I was facing this problem too and tried every other trick from trimming the text to applying the 'text-index' property (using css) :-D :-D. (how silly of me). Problem occured because I indented the code in html :-?... Thanks, your answer helped me too.. :-)Deformation
Also, use the value attribute instead of adding text inside the tags.Complaisance
An oldie but a goodie. This helped me out today. @Surbase you are awesome!Fabriane
@JoãoCunha, there is no value attribute on textarea element. The above solution worked like a charm.Eatton
I've been caught to. This is fun to see so many people falling for this simple mistake.Marinetti
I got caught out by this in Coldfusion. I can't believe whitespace in the code translates into whitespaces in the textarea!Hevesy
Couple years later, but João Cunha, that doesn't work. Didn't for me at least.Cark
Also make sure the </textarea> is not in the next line. it should be like <textarea><?php value ?></textarea>. I forgot about the close tag and roamed in the browser for a long time.Pompidou
Helped me out today, I really appreciate that.Singlehanded
Thank you for this, I never knew about this behavior until today.Psychomancy
Had the same problem and got solved by the above answer. Kudos !!Profusive
I cannot believe that this was the solution to the problemAttila
Coming here in 2021, whitespace from bad formatting on my part was the trick!Aquiculture
Yes it works but why Laravel does not trim the white space???Heroic
wow... really can't see the wood for the trees.Housefather
It is very different situation :D But it works, thanks dudeAsthenosphere
Dude after hours you saved my head from getting smashed to the wall... Jk now for real dude thanks!Kickstand
I think this issue will plague developers for the foreseeable future.... Thanks!Peon
A
113

Well, everything between <textarea> and </textarea> is used as the default value for your textarea box. There is some whitespace in your example there. Try to eliminate all of that.

Make sure that the following is the case:

<textarea><?php 

    *more php code here...*

?></textarea> 
Akilahakili answered 4/2, 2010 at 20:36 Comment(1)
thanks a lot. I didnt realize that everything in between is the default. I chose the guy on top cause he answered it first though he ridiculed me. Thanks for sticking out for folks.Purveyor
E
75

Open (and close!) your PHP tags right after, and before, your textarea tags:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php
  if($siteLink_val) echo $siteLink_val;
?></textarea>
Eclat answered 4/2, 2010 at 20:37 Comment(3)
this a clever way to keep the indentation in these cases. Thanks!Unattended
This finally fixed my issue. Thanks a lot BartUppermost
brilliant. This is very useful in cases where you want to keep a consistent indentation across your blade file.Oboe
G
59

In short: <textarea> should be closed immediately on the same line where it started.


General Practice: this will add-up line-breaks and spaces used for indentation in the code.

<textarea id="sitelink" name="sitelink">
</textarea>

Correct Practice

<textarea id="sitelink" name="sitelink"></textarea>
Galina answered 5/12, 2015 at 7:2 Comment(4)
Solved my problem.Spracklen
This was the perfect solution for me as wellRabia
Solved my problem, great solution. ThanksKiakiah
Works like charm!Maroney
B
30

Basically it should be

<textarea>something here with no spaces in the begining</textarea>

If there are some predefined spaces lets say due to code formatting like below

<textarea>.......
....some_variable
</textarea>

The spaces shown by dots keeps on adding on each submit.

Break answered 28/3, 2014 at 7:23 Comment(4)
This is an old "trick", even sometimes forgotten. We already had HTML issues based on this going back to IE6/7 .. +1Cloudless
Saved my day. Thanks!Franzoni
It works just fine, but it's really funny. Any explanation on this weird bug?Tiannatiara
how do you annotate placeholders, value, methods etc in this setup? I need a place holder, but my tag needs a space between <textarea and placeholder... Removing all possible spaces in the tag leaves me with one whitespece in the browser... I fear that may be the one :(Izabel
L
12

Any space in between textarea openning and closing tags will be consider as whitespace. So for your above code, the correct way will be :

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo $siteLink_val; ?></textarea>
Lesh answered 2/12, 2016 at 11:56 Comment(0)
F
10

I got the same problem, and the solution is very simple: don't start a new line! Although some of the previous answers can solve the problem, the idea is not stated clearly. The important understanding is, to get rid of the unintended spaces, never start a new line just after your start tag.

The following way is WRONG and will leave a lot of unwanted spaces before your text content:

 <textarea>
    text content // start with a new line will leave a lot of unwanted spaces
 </textarea>

The RIGHT WAY to do it is:

 <textarea>text content //put text content right after your start tag, no new line
 </textarea>
Farkas answered 20/5, 2019 at 16:22 Comment(5)
Ok but why Laravel does not trim that white space?Heroic
To Camo: The spaces are trimmed on some platforms but not on others. On some platforms, the old versions may not trim the spaces, while the new ones may do. Even within some of the same versions, it depends. When you have the problem, however, the above stated is one of the ways to solve it.Farkas
I dont understand it. I have Laravel 8 and it is the same. I think trim() should be platform independent.Heroic
Sorry Camo. It was over a year and a half ago, and I forgot the context of the problem. Anyway, if you have the problem, the above stated is one of the ways to solve it.Farkas
@WilliamHou, yes whatever u suggested is worked for me, TQBenefactress
C
9

Another work around would be to use javascript:

//jquery
$('textarea#someid').html($('textarea#someid').html().trim());

//without jquery
document.getElementById('someid').innerHTML = document.getElementById('someid').innerHTML.trim();

This is what I did. Removing white-spaces and line-breaks in the code makes the line too long.

Cellar answered 31/10, 2013 at 10:29 Comment(0)
O
7

I know its late but may help others.

use this in when document indentation is required.

$(document).ready(function()
{
    $('textarea').each(function(){
            $(this).val($(this).val().trim());
        }
    );
});

same question

Ophiology answered 27/6, 2017 at 6:49 Comment(1)
Or you could format your html correctly. But I do agree that can be hard something if you want something like php code inside of it. So I do agree with your answer.Immobile
C
5

To make it look a bit cleaner, consider using the ternary operator:

<textarea><?=( $siteLink_val ? $siteLink_val : '' );?></textarea>
Commie answered 4/2, 2010 at 20:43 Comment(2)
Don't use short tags, don't suggest others to do so. This will help people avoid PITAs when putting some webapp in a production server with a different config. Thank you.Dib
I always use short tags in templating scenarios precisely because I want more people to use them, and thus encourage the PHP community to continue to support them. That said, short tags should ONLY be used in templating scenarios, NEVER in application logic, and quite obviously, ONLY when the server supports them. Always know your production environment before deploying. (Naturally, this is not the place to discuss the pros and cons of short tags, but you brought it up, so that's my justification.)Commie
R
5

simply put all that on one-line

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo $siteLink_val; ?></textarea>
Robbie answered 28/2, 2021 at 21:43 Comment(0)
G
4

Please make sure there is no linebreak or space after , it's to make sure there is no whitespace or tab, just copy and paste this code :) I had fix it for you

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo trim($siteLink_val);?></textarea>
Gnotobiotics answered 7/3, 2012 at 13:47 Comment(0)
B
4
<textarea style="width:350px; 
 height:80px;" cols="42" rows="5" name="sitelink"
 ><?php if($siteLink_val) echo $siteLink_val; ?></textarea> 

Moving ...> down works for me.

Burnt answered 21/11, 2016 at 18:36 Comment(0)
S
4

Just define your and your close tag in same line.

<textarea class="form-control"
          id="newText"
          rows="6"
          placeholder="Your placeholder here..."
          required
          name="newText"></textarea>
Swathe answered 24/5, 2019 at 14:49 Comment(0)
I
2

Furthermore: the textarea tag shows spaces for new lines, tabs, etc, in multiline code.

Iridescent answered 23/1, 2013 at 11:21 Comment(0)
P
2

keep textarea code with no additional white spaces inside

moreover if you see extra blank lines there is solution in meta language:

<textarea>
for line in lines:
echo line.replace('\n','\r')
endfor
</textarea>

it will print lines without additional blank lines of course it depends if your lines ending with '\n', '\r\n' or '' - please adapt

Proverbial answered 29/1, 2013 at 9:48 Comment(0)
P
2

The text area shows mysterious spaces because there is a real space exists in the tags. <textarea> <php? echo $var; ?> </textarea> after removing these extra spaces between the tags will solve the issue , as following. <textarea><php? echo $var; ?></textarea>

Pelagianism answered 29/7, 2013 at 6:6 Comment(0)
E
2

One solution that has worked for me is adding the style white-space: normal; to the textarea because at times it is not feasible to eliminate all the whitespace (for example when you want your code to abide to your coding guidelines which requires adding tabs, whitespaces and line breaks)

Please note that the default for textarea, at least in chrome is: white-space: pre-wrap;

Edmea answered 16/10, 2014 at 5:0 Comment(0)
P
2

If you still like to use indentation, do it after opening the <?php tag, like so:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php // <--- newline
    if($siteLink_val) echo $siteLink_val; // <--- indented, newline
?></textarea>
Parabola answered 14/6, 2016 at 7:57 Comment(0)
E
1

I'm against HTML code mixed with PHP code.

However try this:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink">
<?php 
    if($siteLink_val) 
        echo trim($siteLink_val);
?> 
</textarea>
Emma answered 4/2, 2010 at 20:50 Comment(2)
Almost... that still includes two newlines.Akilahakili
Should use <textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php and ?></textarea> to ensure that it is truly pure. Also, may I ask why you're against HTML with PHP?Insoluble
C
0

Also, when you say that the cursor is in the "middle" of the textarea, it makes me think you could also either have extra padding or text-align: center defined somewhere in your CSS.

Commie answered 4/2, 2010 at 20:46 Comment(0)
S
0

Make sure first that your $siteLink_val isn't returning white space as a value. The <textarea> element by default has an empty value so if the variable you're echo'ing for some reason has spaces, there's your problem right off the bat.

To make the code the absolute cleanest, I would suggest you could do something like this, allowing for some more flexibility later. I've made a function that returns either a NULL if the variable isn't present (what you seem to be aiming for in the original post) and the absolute value otherwise. Once you've made sure of your variable's contents, try this:

function build_siteLink_val() {
     if ( $siteLink_val ) {
          return $siteLink_val;
     }

     else {
          return "";
     }
}

$output_siteLink_val = build_siteLink_val();

And the following code in your textarea would now read:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?=$output_siteLink_val?></textarea>

This is assuming your PHP install is configured for short-hand variable calls, as seen in the shortened "<?=?>" tags. If you cannot output this way, remember to preface your PHP code with "<?php" and close with "?>".

Avoid line breaks between <textarea>'s because it can create the potential of erroneous characters.

Also, check your CSS to make sure there isn't a padding rule pushing text inward.

Also, you specify a cols and rows value on the textarea, and then style a width and height. These rules are counter-productive, and will result in inconsistent visuals. Stick with either defining the size through style (I recommend giving the element a class) or the rows/cols.

Scissel answered 4/2, 2010 at 20:54 Comment(0)
I
0

In addition to doing this:

Before:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink">
    <?php if($siteLink_val) echo $siteLink_val; ?> 
</textarea>

after:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo $siteLink_val; ?></textarea>

I too added the method trim() of PHP:

<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo trim($siteLink_val); ?></textarea>
Inainability answered 22/7, 2021 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.