Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2004, 02:09 AM   PM User | #1
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
innerText equivalent for Mozilla

the scenario...
getting the content of a div then putting that string in a textarea.
the problem is that there are links in the content.. which I thought I could solve by making the function recursive but it seems to not act as I had expected. IE returns exactly what I need.

Also, how would I be able to get the newline to be passed as well ?

BTW, I did try searching.. and I think my issue is probably more with the recursiveness since it seems to end after getInnerText() is called again.
Code:
var txt
function getInnerText(o)
{
    children    = o.childNodes
    children_len= children.length
    for (var i=0; i<children_len; i++) {
        if (children.item(i).childNodes.length>0) {
            getInnerText(children.item(i))
        } else if (children.item(i).nodeType==3) {
            txt += children.item(i).nodeValue
        }
    }
}
here is a sample div:
Code:
<div id="text_3" onClick="getInnerText(this);alert(txt)" style="cursor:pointer;cursor:hand;font-family:Times;font-size:8pt">31sadfasdf <a href="http://www.domain.com/?msg=21&link=http%3A%2F%2Fwww.test.com%2F">http://www.test.com/</a><br>adsfasdfadsf<br>asdfadsf<br>adsfadsf</div>
fci is offline   Reply With Quote
Old 09-03-2004, 03:12 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
Sorry mis-read your post
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.

Last edited by Mr J; 09-03-2004 at 03:14 PM..
Mr J is offline   Reply With Quote
Old 09-03-2004, 04:11 PM   PM User | #3
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
I posted this on a mailing list I'm on and someone was able to help me..
he replied with:
Quote:
You aren't returning anything from recursion.

// untested, but should do the trick
function getInnerText(o)
{
var txt = "";
var i;

for(i = 0; i < o.childNodes.length; i++)
{
if(o.childNodes[i].nodeType == 1 /* ELEMENT_NODE */)
txt += getInnerText(o.childNodes[i]);
if(o.childNodes[i].nodeType == 3 /* TEXT_NODE */)
txt += o.childNodes[i].nodeValue;
}

return(txt);
}

As far as newlines go: you are, in general, not guaranteed anything
about whitespace in HTML - you may or may not get newlines depending
on browser & JavaScript implementation. The only way you can
force whitespace/newline preservation is enclosing the relevant text
in <![CDATA[ ... ]]> tokens,

-Bob
so... I ended up with this:
Code:
function getInnerText(o)
{
    var txt='';
    for (var i=0; i<o.childNodes.length; i++) {
        switch(o.childNodes[i].nodeType) {
            case 1 :    txt += getInnerText(o.childNodes[i]);   break
            case 3 :    txt += o.childNodes[i].nodeValue;       break
            case 8 :    txt += "\n";                            break
        }

    }
    return txt;
}
I'm using comment nodes(<!--#-->) to signify the \n.
fci is offline   Reply With Quote
Old 05-07-2008, 10:22 AM   PM User | #4
chatterjee_yash
New to the CF scene

 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
chatterjee_yash is an unknown quantity at this point
You can use textContent in place on innerText

Hi,

You can use "textContent" in mozilla which is equivalent to "innerText" in IE.


.........
Yash.
chatterjee_yash is offline   Reply With Quote
Old 05-07-2008, 08:35 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You don't need a recursion. You may simply use the getElementsByTagName() method -with a wild card as argument - and nextSibling(). You may also remove the gaps or the carriage return on using RegExp.

Could be something like this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function addText(obj,txt){
obj.firstChild?obj.firstChild.nodeType==3?txt+=(obj.firstChild.nodeValue+' '):null:null;

return txt;
}
function getInnerText(obj){

var txt='';
txt=addText(obj,txt)
var ele=obj.getElementsByTagName('*'), i=0, e;
while(e=ele[i++]){
txt=addText(e,txt);
e.nextSibling?e.nextSibling.nodeType==3?txt+=(e.nextSibling.nodeValue+' '):null:null;
}
txt=txt.replace('\n','','g').replace(/\s{1,}/g,' ').replace(/\s$/,'');//removes the gaps and the last space
return txt;
}
</script>
<body>
<div id="text_3" onclick="alert(getInnerText(this))" style="cursor:pointer;cursor:hand;font-family:Times;font-size:8pt">

31sadfasdf
<div>foo<span>fee </span></div>

 <a href="http://www.domain.com/?msg=21&link=http%3A%2F%2Fwww.test.com%2F">http://www.test.com/</a><br>adsfasdfadsf

<br>asdfadsf
<br>adsfadsf
</div>
</body>
</html>
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 05-07-2008 at 09:12 PM..
Kor is offline   Reply With Quote
Old 05-07-2008, 11:58 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
far simpler and slightly faster crossbrowser code:

Code:
function getInnerText(o)
{
  return o.textContent ? o.textContent : o.innerText
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:18 AM.


Advertisement
Log in to turn off these ads.