0

I'm wondering why javascript/jquery removes my linebreaks (\n) when I copy a textarea's value (id="t") to another element (id="t_hidden").

$('#t_hidden').val($('#t').val());

When I do sth. with value of $('#t_hidden') I recognize that my linebreaks have disappeared. Does anybody know why?

David 10K
  • 303
  • 2
  • 14
  • 3
    what element types are we talking here? – epascarello Sep 05 '12 at 19:19
  • 1
    you could probably to .html() instead of .val() depending on what type of elements your talking about – Scott Selby Sep 05 '12 at 19:19
  • Related: [Post newline/carriage return as hidden field value](http://stackoverflow.com/questions/667915/post-newline-carriage-return-as-hidden-field-value) – Orbling Sep 05 '12 at 19:22
  • @epascarello O thanks! I think your question has solved my problem. It was copied to an input element. This will maybe automatically remove the linebreaks. – David 10K Sep 05 '12 at 19:22

2 Answers2

5

Is the destination element a textarea? If not, the linebreaks could be copied, but ignored as whitespace. You'd need to replace them with a <br /> to have them work.

newtron
  • 5,938
  • 1
  • 23
  • 19
3

Elements with the type of hidden and textboxes so not have line breaks, they will be removed when you set the value. If you want to maintain the linebreaks, use a textarea and set the display to none.

epascarello
  • 204,599
  • 20
  • 195
  • 236