0

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.

8
  • 2
    That's because it is one continuous string. You are not adding line breaks, you are just causing wrapping within the text box. The value contained by this textbox will still be a continuous string, so you will need to provide your own line breaking if that is what is required. Commented Nov 14, 2012 at 9:04
  • 1
    does not happen for me ... here is a screenshot: imgur.com/ZdTvN Commented Nov 14, 2012 at 9:05
  • It is one line, it's just wrapped in the textarea because it is not wide enough. I guess you can always split the string every 30 characters (and some log to avoid splitting words) if you really want that. Commented Nov 14, 2012 at 9:05
  • can you show your script of going to next line Commented Nov 14, 2012 at 9:08
  • @Sibu: Can you please check my fiddle link Commented Nov 14, 2012 at 9:14

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah..I know that part.But What about if it wraps to next line? Does't it consider them as multiple lines?
No. Wrapping is not a line break. Like I said, a line break is defined as \n and wrapping is... well... wrapping and nothing else.
1

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>

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.