views:

29

answers:

2

problem with IE which removes new lines from $("#content").text()

HTML Code

<div id="content">
<p>hello world</p>
<p>this is a paragraph</p>
</div>

jQuery Code

alert($("#content").text()); 

result (IE) IE removes new line (\n) how can i fix this problem ?

hello worldthis is a paragraph

result (FF)

hello world
this is a paragraph

take a look : http://jsfiddle.net/vB3bx/

A: 

Not sure you're going to find a solution using text() if you consider the following:

Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.

at http://api.jquery.com/text/

simnom
A: 

It works fine if you use the div's innerText property. Try replacing

alert($("#content").text());

with

alert( document.getElementById( "content" ).innerText );
Sparafusile
undefined ! http://jsfiddle.net/vB3bx/1/
faressoft
@faressoft - uh... what?
Sparafusile