2

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

Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
  • Hi you can remove all unwanted spaces before set the value.. mystring = mystring.replace(/(^\s+|\s+$)/g,' '); [Click here to know more](http://stackoverflow.com/questions/6163169/removing-whitespace-from-string-in-javascript)... May this will help you .... – Basith Nov 23 '12 at 10:59
  • here you are replacing it with space, i dont want to lose my data with new new lines, i did this
    but it didn't work either
    – Raheel Sadiq Nov 23 '12 at 11:39

1 Answers1

2

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>​
Cerbrus
  • 70,800
  • 18
  • 132
  • 147