473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Using Dom Tree structure instead of innerHTML

CES
All,
Visual Studio 2005 doesn't include an auto complete item for innerHTML document.getElementById("SomeID").innerHTML, is their a way of referencing the inner text of an element without using ???.innerHTML, that will work in all browsers.

The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";

Thanks in advance. - CES
Jul 5 '06 #1
6 3047
Ivo
"CES" <no**@none.comsaid
All,
One,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of referencing
the inner text of an element without using ???.innerHTML, that will work
in all browsers.
The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";
It depends on what kind of node the firstChild is. IE is certainly capable
of setting text this way. A big difference between the browsers you mention
is their handling of whitespace textnodes: IE doesn't include textnodes with
only spaces, tabs and/or newlines in the DOM, so the first child is really
the second child to Firefox. Not sure if that 's causing your problem
without seeing more code I'm afraid. What errors occur? You certainly don't
need the innerHTML property.
Or perhaps somebody installed a toolbar in your IE which filters possibly
objectionable material, and all you need to do to make it work is feed a
different text.
hth
ivo
Jul 5 '06 #2
CES wrote:
All,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of
referencing the inner text of an element without using ???.innerHTML,
that will work in all browsers.
innerHTML is a property of an element, its value is as string. I suppose
the only 'logical' auto-complete would be string methods.

The W3C equivalent to innerText is textContent, but that it's not
supported by IE or older browsers - although most modern browsers do.
Probably the most widely supported method is to recurse down an
element's child nodes and grab the data property of text nodes.

But if innerText or textContent are supported, why not use them based on
feature detection? Try:

function getElText(el)
{
if (el.textContent) return el.textContent;
if (el.innerText) return el.innerText;
var x = el.childNodes;
var txt = '';
for (var i=0, len=x.length; i<len; ++i){
if (3 == x[i].nodeType) {
txt += x[i].data;
} else if (1 == x[i].nodeType){
txt += getElText(x[i]);
}
}
return txt.replace(/\s+/g,' ');
}

The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";
It is not a good idea to guess what the firstChild (or any particular
node) will be unless you have complete control over the HTML. And even
then, it makes for brittle code.
--
Rob
Jul 5 '06 #3
CES
Ivo wrote:
"CES" <no**@none.comsaid
>All,

One,
>Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of referencing
the inner text of an element without using ???.innerHTML, that will work
in all browsers.
The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.node Value = "XXX";

It depends on what kind of node the firstChild is. IE is certainly capable
of setting text this way. A big difference between the browsers you mention
is their handling of whitespace textnodes: IE doesn't include textnodes with
only spaces, tabs and/or newlines in the DOM, so the first child is really
the second child to Firefox. Not sure if that 's causing your problem
without seeing more code I'm afraid. What errors occur? You certainly don't
need the innerHTML property.
Or perhaps somebody installed a toolbar in your IE which filters possibly
objectionable material, and all you need to do to make it work is feed a
different text.
hth
ivo

ivo,
Thanks it's a whitespace issue... however I still have the same question, Is their a way of replicating what innerHTML does using the DOM Tree?
Just for future reference, how would you refer to an element with no whitespace such as:
<div id="SomeID"></div>
and same question with whitespace:
<div id="SomeID">
</div>
If you know of any Tutorials re: whitespace id be great full.
Thanks - CES
-
Jul 5 '06 #4
CES wrote:
All,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of
referencing the inner text of an element without using ???.innerHTML,
that will work in all browsers.

The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";

Thanks in advance. - CES
I once wrote a function called "flatten" to do this - which would
basically recurse all children of a node and aggregate their contents,
returning plain text with no markup. I can't find the code, but it went
something like this:

//code starts here
function flatten(el)
{
if(el.nodeType == 3)
return el.nodeValue;

var result = "";

for(var i = 0; i < el.childNodes.length; i++)
result += flatten(el);

return result;
}

var text = flatten(document.getElementById("foo"));
alert("Here's an annoying alert box with the flat text: " + text);
//code ends here

Beware of trying this on a really complex fragment. Too much recursion
does not do a body good.

Jeremy
Jul 5 '06 #5
Jeremy wrote:
//code starts here
function flatten(el)
{
if(el.nodeType == 3)
return el.nodeValue;

var result = "";

for(var i = 0; i < el.childNodes.length; i++)
result += flatten(el);

return result;
}

var text = flatten(document.getElementById("foo"));
alert("Here's an annoying alert box with the flat text: " + text);
//code ends here
This is buggy.

Should be:
result += flatten(el.childNodes[i]);

Sorry.

Jeremy
Jul 5 '06 #6
CES wrote:
Ivo wrote:
"CES" <no**@none.comsaid
All,
One,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of referencing
the inner text of an element without using ???.innerHTML, that will work
in all browsers.
The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";
It depends on what kind of node the firstChild is. IE is certainly capable
of setting text this way. A big difference between the browsers you mention
is their handling of whitespace textnodes: IE doesn't include textnodes with
only spaces, tabs and/or newlines in the DOM, so the first child is really
the second child to Firefox. Not sure if that 's causing your problem
without seeing more code I'm afraid. What errors occur? You certainly don't
need the innerHTML property.
Or perhaps somebody installed a toolbar in your IE which filters possibly
objectionable material, and all you need to do to make it work is feed a
different text.
hth
ivo
ivo,
Thanks it's a whitespace issue... however I still have the same question, Is their a way of replicating what innerHTML does using the DOM Tree?
Just for future reference, how would you refer to an element with no whitespace such as:
<div id="SomeID"></div>
and same question with whitespace:
<div id="SomeID">
</div>
If you know of any Tutorials re: whitespace id be great full.
Gecko browsers (Mozilla, Firefox, etc.) will collapse whitespace
according to the relevant W3C spec and may replace it with #text nodes,
see:

<URL:http://developer.mozilla.org/en/docs/Whitespace_in_the_DOM>
I may have missed the point a bit in my response to your question
about:

document.getElementById("SomeID").firstChild.nodeV alue = "XXX";
If there is no firstChild, IE will error. e.g. if "SomeID" is like:

<div id="SomeID"></div>
Then there is no firstChild and IE errors. From memory, if the content
is only whitespace IE will collapse and remove it and so there still
won't be a firstChild. Put something "non-collapsible" in there and
you're OK:

<div id="SomeID">&nbsp;</div>
A general solution is to cleanout the contents, then write your own
textNode:

var el = document.getElementById("SomeID");
while (el.firstChild) {
el.removeChild(el.firstChild);
}
el.appendChild(document.createTextNode('blah'));
In this case, innerHTML seems much simpler. :-)

--
Rob

Jul 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: shane | last post by:
From searching around, Ive seen this question asked numerous times, but havent seen a usable answer unfortuantly. What Im trying to do is something I would have thought would be quite common,...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
3
by: adam | last post by:
Hi, I'm building a tree control that lazily loads branches of the tree using the document.load() method. The external XML document that is loaded is generated by a servlet as XHTML. What I...
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
3
by: piotrek | last post by:
Hi I would like to ask you a question. Ian creating app. that download from server directory structure ( whole tree ) and those data are placed in proper places into my treeview control. I...
3
by: uday.das | last post by:
hi , I am not sure but I think it might be to avoid heap fragmentation due to several malloc calls. Are there any strong reason ? What are the other things that should take care when we implement...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.