Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

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



question from:https://stackoverflow.com/questions/65893245/textarea-is-removing-new-line-only-if-it-comes-first

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...