Why is textarea filled with mysterious white spaces?
PhpHtmlFormsTextareaPhp Problem Overview
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?
Php Solutions
Solution 1 - Php
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.
Solution 2 - Php
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.
Solution 3 - Php
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>
Solution 4 - Php
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>
Solution 5 - Php
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.
Solution 6 - Php
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>
Solution 7 - Php
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.
Solution 8 - Php
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>
Solution 9 - Php
To make it look a bit cleaner, consider using the ternary operator:
<textarea><?=( $siteLink_val ? $siteLink_val : '' );?></textarea>
Solution 10 - Php
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());
}
);
});
Solution 11 - Php
Please make sure there is no linebreak or space after
Solution 12 - Php
<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.
Solution 13 - Php
Just define your
Solution 14 - Php
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>
Solution 15 - Php
Furthermore: the textarea tag shows spaces for new lines, tabs, etc, in multiline code.
Solution 16 - Php
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
Solution 17 - Php
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>
Solution 18 - Php
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;
Solution 19 - Php
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>
Solution 20 - Php
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>
Solution 21 - Php
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.
Solution 22 - Php
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.
Solution 23 - Php
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>