I've just ran into a pretty strange behaviour of multiline text in firefox using knockoutjs bindings. Here is my fiddle: http://jsfiddle.net/NznVZ/.
We have a textarea and span with value/text binding to the same observable. Currently, Chrome and IE do display a multiline text in span element, but firefox doesn't (it just concatenates several lines into 1).
Can someone explain what is/where is the problem? Maybe someone had already run into this issue and has a solution?
Thanks in advance
P.S. Firefox 12, IE 9, Chrome 18
Setting the
white-space: pre-wrap
style on the span will make it work: http://jsfiddle.net/mbest/NznVZ/12/Here's a little background. IE and Chrome will convert the newlines in the string to
<br>
elements in the HTML when the text is set usinginnerText
, which is what Knockout uses. Firefox doesn't haveinnerText
so Knockout usestextContent
, which doesn't do any conversion of the string. (Interestingly, Chrome matches Firefox when you use thewhite-space: pre-wrap
style.)IE:
Chrome (without white-space style):
Firefox and Chrome (with white-space style):
If you want a javascript solution: http://jsfiddle.net/9KAQX/
The only advantage here is that "\n" characters are not present in the span.