Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript setting textarea value with new line

Tags:

javascript

php

i am new to php, javascript and web

I am having a situation => I have to set a value of text field on my client side, i pass value to my javascript function and it sets value as

 function editSubService(description){
      setVal("sub_service_description", description);
 } 

and it sets the value right , but if there is new line in the text, it does not set the text field. how can this set that value of text field, its only not working if new line

like image 850
Raheel Sadiq Avatar asked May 02 '26 23:05

Raheel Sadiq


1 Answers

A <input> element with type="text" will not accept any newline characters. It is, by definition, a single-line field.
Use a <textarea> if you wish to have a multi-line input.

It's specification for type="text", according to MDN:

text: A single-line text field; line-breaks are automatically removed from the input value.

Example:

var area = document.getElementById("sub_service_description");
area.value = "This is some\ntext with a newline character";
console.log(area.value);
<textarea id="sub_service_description"></textarea>​
like image 75
Cerbrus Avatar answered May 04 '26 13:05

Cerbrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!