hotwheelharry
05-13-2010, 04:00 AM
My question is how or what property do I use to get the text inside of elements (nested or unnested) with all the new line characters?
I dont want to have to write a function that takes apart all the elements and does this manually.
So far, only Chrome seems to support this property correctly.
I have some code...
<!--Head-->
<body>
<div id="root">
<br/>
<div>this is <span style="color:red">red </span>text</div>
<p>this is another line</p>
</div>
<textarea id="txt" style="width:300px; height:300px;"></textarea>
</body>
<script type="text/javascript">
var txt = document.getElementById("txt");
var root = document.getElementById("root");
txt.value = root.innerText;
var c = 0;
var r = 0;
for(var i = 0; i < txt.value.length; i++)
{
if(txt.value[i] == "\n")
{
c++;
}
if(txt.value[i] == "\r")
{
r++;
}
}
alert(c + "\n" +r); // output the line character counts
</script>
</html>
Here are the results displayed in the textarea in the following browsers...
Chrome:
all text with \n character separating new lines
FF:
undefined
IE:
all text but NO new line characters
My question is how or what property do I use to get the text inside of elements (nested or unnested) with all the new line characters?
I dont want to have to write a function that takes apart all the elements and does this manually.
So far, only Chrome seems to support this property correctly.
I dont want to have to write a function that takes apart all the elements and does this manually.
So far, only Chrome seems to support this property correctly.
I have some code...
<!--Head-->
<body>
<div id="root">
<br/>
<div>this is <span style="color:red">red </span>text</div>
<p>this is another line</p>
</div>
<textarea id="txt" style="width:300px; height:300px;"></textarea>
</body>
<script type="text/javascript">
var txt = document.getElementById("txt");
var root = document.getElementById("root");
txt.value = root.innerText;
var c = 0;
var r = 0;
for(var i = 0; i < txt.value.length; i++)
{
if(txt.value[i] == "\n")
{
c++;
}
if(txt.value[i] == "\r")
{
r++;
}
}
alert(c + "\n" +r); // output the line character counts
</script>
</html>
Here are the results displayed in the textarea in the following browsers...
Chrome:
all text with \n character separating new lines
FF:
undefined
IE:
all text but NO new line characters
My question is how or what property do I use to get the text inside of elements (nested or unnested) with all the new line characters?
I dont want to have to write a function that takes apart all the elements and does this manually.
So far, only Chrome seems to support this property correctly.