Reputation: 3051
Hi,
I am facing trouble while getting data from an html textarea.The textarea is defined as follows:
<textarea id="ta" cols=30 rows=15>
</textarea>
Here,the textarea is restricted to 30 chars.So,if the no of characters per line exceeds 30 chars,the cursor will move to next line.It works well.But the problem,when I am trying to get that value using javascript
,the entire textarea content is shown as single line(continuous string) though it has mulitple.
Here is my fiddle
It shows mulitple lines,if line is separated by ENTER
key.
Upvotes: 0
Views: 562
Reputation: 201588
By default, browsers automatically line-wrap the text if needed, but such wrapping is just visual, or “soft”. It does not affect the data sent to a server (when the form is submitted) or the internal representation.
If you use the wrap=hard
attribute in textarea
, then automatic wrapping becomes “hard” in the sense of adding line breaks to the submitted data. But on the browsers I tested, the line breaks do not appear in the internal representation that you can access in JavaScript. (Presumably, the browser re-wraps the text upon submission.)
So if you really need to get the text wrapped, as accessed in JavaScript, you need to do it yourself. On the other hand, as the automatic wrapping is not actual user input, there’s seldom reason to treat it as if it were.
Upvotes: 1
Reputation: 46
Press "enter key" it will be included in the alert.
Even if with "new lines" still the value is a single string. example: "Stack Overflow" without pressing enter.
"Stack[ENTER KEY]Overflow" pressing enter... still results a string "Stack\nOverflow"
var x = "Stack\nOverflow";
alert(x);
Element attribute COLS -> column, while ROWS -> rows, much like width and height, but not really.
<textarea id="ta" cols=30 rows=15></textarea>
Upvotes: 1
Reputation: 2060
If you press ENTER
when inputing to a <textarea>
this will insert a line break. A line break in JavaScript is \n
. You shouldn't need to do anything with this, JavaScript should always treat it as a line break. Just like for jsshah, the code you pasted works fine for me too.
Upvotes: 2