jQuery data received from server does not keep original format using append()
up vote
0
down vote
favorite
I am receiving data from the server and displaying it to the user. I originally was displaying the data using a textarea
and all the formatting is correct (line breaks, tabs...etc).
html:
<textarea id="textArea" class="textArea" readonly></textarea>
js:
//data stream received from server
...
data = xhr.responseText;
$( ".textArea" ).val( data );
The above works fine, however, now I want to switch my textarea
to a div
instead (to add styles and be able to print the div later...), but after I switched all my formatting received from the server are gone and all the data is displayed in a whole blob when I use append:
html:
<div id="textArea" class="textArea"></div>
js:
//data stream received from server
...
data = xhr.responseText;
$(".textArea").append(data);
How can I keep the source formatting when appending data to div
?
javascript jquery html
add a comment |
up vote
0
down vote
favorite
I am receiving data from the server and displaying it to the user. I originally was displaying the data using a textarea
and all the formatting is correct (line breaks, tabs...etc).
html:
<textarea id="textArea" class="textArea" readonly></textarea>
js:
//data stream received from server
...
data = xhr.responseText;
$( ".textArea" ).val( data );
The above works fine, however, now I want to switch my textarea
to a div
instead (to add styles and be able to print the div later...), but after I switched all my formatting received from the server are gone and all the data is displayed in a whole blob when I use append:
html:
<div id="textArea" class="textArea"></div>
js:
//data stream received from server
...
data = xhr.responseText;
$(".textArea").append(data);
How can I keep the source formatting when appending data to div
?
javascript jquery html
2
Can you use<pre>
? Or addwhite-space: pre;
to the<div>
.
– azeós
Nov 16 at 23:52
@azeós you mean like this$(".textArea").append(<pre>data</pre>);
?
– user10067412
Nov 16 at 23:56
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am receiving data from the server and displaying it to the user. I originally was displaying the data using a textarea
and all the formatting is correct (line breaks, tabs...etc).
html:
<textarea id="textArea" class="textArea" readonly></textarea>
js:
//data stream received from server
...
data = xhr.responseText;
$( ".textArea" ).val( data );
The above works fine, however, now I want to switch my textarea
to a div
instead (to add styles and be able to print the div later...), but after I switched all my formatting received from the server are gone and all the data is displayed in a whole blob when I use append:
html:
<div id="textArea" class="textArea"></div>
js:
//data stream received from server
...
data = xhr.responseText;
$(".textArea").append(data);
How can I keep the source formatting when appending data to div
?
javascript jquery html
I am receiving data from the server and displaying it to the user. I originally was displaying the data using a textarea
and all the formatting is correct (line breaks, tabs...etc).
html:
<textarea id="textArea" class="textArea" readonly></textarea>
js:
//data stream received from server
...
data = xhr.responseText;
$( ".textArea" ).val( data );
The above works fine, however, now I want to switch my textarea
to a div
instead (to add styles and be able to print the div later...), but after I switched all my formatting received from the server are gone and all the data is displayed in a whole blob when I use append:
html:
<div id="textArea" class="textArea"></div>
js:
//data stream received from server
...
data = xhr.responseText;
$(".textArea").append(data);
How can I keep the source formatting when appending data to div
?
javascript jquery html
javascript jquery html
asked Nov 16 at 23:43
user10067412
888
888
2
Can you use<pre>
? Or addwhite-space: pre;
to the<div>
.
– azeós
Nov 16 at 23:52
@azeós you mean like this$(".textArea").append(<pre>data</pre>);
?
– user10067412
Nov 16 at 23:56
add a comment |
2
Can you use<pre>
? Or addwhite-space: pre;
to the<div>
.
– azeós
Nov 16 at 23:52
@azeós you mean like this$(".textArea").append(<pre>data</pre>);
?
– user10067412
Nov 16 at 23:56
2
2
Can you use
<pre>
? Or add white-space: pre;
to the <div>
.– azeós
Nov 16 at 23:52
Can you use
<pre>
? Or add white-space: pre;
to the <div>
.– azeós
Nov 16 at 23:52
@azeós you mean like this
$(".textArea").append(<pre>data</pre>);
?– user10067412
Nov 16 at 23:56
@azeós you mean like this
$(".textArea").append(<pre>data</pre>);
?– user10067412
Nov 16 at 23:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
What you're really looking for is a way to make a div
behave like a textarea
, because a textarea
renders text in a fixed-width font and remembers whitespace.
.textarea {
border: 1px solid gray;
font-family: monospace;
height: 200px;
overflow: auto;
padding: 2px;
resize: both;
width: 400px;
white-space: pre;
}
https://jsfiddle.net/AnonymousSB/hkfy14es/
Note: Unlike a textarea
, you have to keep the first line of text on the same line as your opening div
element to prevent a gap at the top, because white-space: pre;
makes it render ALL white space.
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
What you're really looking for is a way to make a div
behave like a textarea
, because a textarea
renders text in a fixed-width font and remembers whitespace.
.textarea {
border: 1px solid gray;
font-family: monospace;
height: 200px;
overflow: auto;
padding: 2px;
resize: both;
width: 400px;
white-space: pre;
}
https://jsfiddle.net/AnonymousSB/hkfy14es/
Note: Unlike a textarea
, you have to keep the first line of text on the same line as your opening div
element to prevent a gap at the top, because white-space: pre;
makes it render ALL white space.
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
add a comment |
up vote
2
down vote
accepted
What you're really looking for is a way to make a div
behave like a textarea
, because a textarea
renders text in a fixed-width font and remembers whitespace.
.textarea {
border: 1px solid gray;
font-family: monospace;
height: 200px;
overflow: auto;
padding: 2px;
resize: both;
width: 400px;
white-space: pre;
}
https://jsfiddle.net/AnonymousSB/hkfy14es/
Note: Unlike a textarea
, you have to keep the first line of text on the same line as your opening div
element to prevent a gap at the top, because white-space: pre;
makes it render ALL white space.
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
What you're really looking for is a way to make a div
behave like a textarea
, because a textarea
renders text in a fixed-width font and remembers whitespace.
.textarea {
border: 1px solid gray;
font-family: monospace;
height: 200px;
overflow: auto;
padding: 2px;
resize: both;
width: 400px;
white-space: pre;
}
https://jsfiddle.net/AnonymousSB/hkfy14es/
Note: Unlike a textarea
, you have to keep the first line of text on the same line as your opening div
element to prevent a gap at the top, because white-space: pre;
makes it render ALL white space.
What you're really looking for is a way to make a div
behave like a textarea
, because a textarea
renders text in a fixed-width font and remembers whitespace.
.textarea {
border: 1px solid gray;
font-family: monospace;
height: 200px;
overflow: auto;
padding: 2px;
resize: both;
width: 400px;
white-space: pre;
}
https://jsfiddle.net/AnonymousSB/hkfy14es/
Note: Unlike a textarea
, you have to keep the first line of text on the same line as your opening div
element to prevent a gap at the top, because white-space: pre;
makes it render ALL white space.
edited Nov 16 at 23:59
answered Nov 16 at 23:58
AnonymousSB
51712
51712
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
add a comment |
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
Thanks that's what i was looking for.
– user10067412
Nov 16 at 23:58
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346793%2fjquery-data-received-from-server-does-not-keep-original-format-using-append%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you use
<pre>
? Or addwhite-space: pre;
to the<div>
.– azeós
Nov 16 at 23:52
@azeós you mean like this
$(".textArea").append(<pre>data</pre>);
?– user10067412
Nov 16 at 23:56