javascript - textarea is removing new line only if it comes first
Hard to explain, but for some reason when I grab content (in php) that contains new lines at the start of a file (Like this):
Hello world!
and echo($content);
it into a <textarea>
; It is shown like this:
Hello world!
It ONLY removes the first line (if there is one that's blank).
But if I do this in JavaScript: textarea.innerHTML = '<?PHP echo($content); ?>'
it works (not ideal tho, I am trying to stear away from this)..
Really could use the help because I am dumbfounded right now :S
EDIT: Also, it only happens if the new lines come before any text/strings. For example this will be retrieved like this and displayed like this:
Hello world, this will work
Answer
Solution:
I believe a newline immediately after the <textarea>
tag is not included in its value. This allows you to write something like:
<textarea name="somename">
this is some
contents
</textarea>
without the value containing a newline before this
; it's treated equivalent to
<textarea name="somename">this is some
contents
</textarea>
So the solution is to always put a newline after <textarea>
before the variable.
<textarea>
<?php echo $content; ?></textarea>
Source