views:

126

answers:

1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<script type="text/javascript">
function init(){
  document.getElementById('test2').innerHTML = '1       2';
}
</script>
<style type="text/css">
#test{
  white-space:pre;
}

#test2{
  white-space:pre;
}
</style>
<title></title>
</head>
<body onload="init();">
<div id="test">
1       2
</div>
<div id="test2"></div>
</body>

</html>

Here is an example page showing my problem. I have two divs. Each has this text '1 2' with white-space. The only difference is that one is injected dynamically and one is in the html itself.

If you open the above page in IE7, the text in div test2 doesn't respect white space. How can i get the same behavior for the text that is dynamically injected??

P.S. My doctype is triggering standards mode on IE7.

A: 

Have you tried substituting white space with &nbsp;?

edl
I dont want to get into modifying that string. Above is just an example. In the actual scenario, that string value is coming from a textarea that the user inputs his text in.So I want to preserver newlines,spaces etc.
Rajat
Hrm. Thats an interesting question. I'll look into it also. Might be useful to know in the future. Have you tried `white-space:pre-wrap;`?
edl
Also try innerTEXT instead of innerHTML. Supposedly that preserves whitespace.
edl
white-space:pre-wrap; doesnt work either. will try using innerText.
Rajat
innerTEXT works fine on IE7. Going to accept this answer as edl mentioned it in the comments.
Rajat