473,785 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Dom Tree structure instead of innerHTML

CES
All,
Visual Studio 2005 doesn't include an auto complete item for innerHTML document.getEle mentById("SomeI D").innerHTM L, 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.getEle mentById("SomeI D").firstChild. nodeValue = "XXX";

Thanks in advance. - CES
Jul 5 '06 #1
6 3070
Ivo
"CES" <no**@none.coms aid
All,
One,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getEle mentById("SomeI D").innerHTM L, 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.getEle mentById("SomeI D").firstChild. nodeValue = "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.getEle mentById("SomeI D").innerHTM L, 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.getEle mentById("SomeI D").firstChild. nodeValue = "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.coms aid
>All,

One,
>Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getEl ementById("Some ID").innerHTM L, 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.getEl ementById("Some ID").firstChild .nodeValue = "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.getEle mentById("SomeI D").innerHTM L, 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.getEle mentById("SomeI D").firstChild. nodeValue = "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.l ength; i++)
result += flatten(el);

return result;
}

var text = flatten(documen t.getElementByI d("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.l ength; i++)
result += flatten(el);

return result;
}

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

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

Sorry.

Jeremy
Jul 5 '06 #6
CES wrote:
Ivo wrote:
"CES" <no**@none.coms aid
All,
One,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getEle mentById("SomeI D").innerHTM L, 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.getEle mentById("SomeI D").firstChild. nodeValue = "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.mozil la.org/en/docs/Whitespace_in_t he_DOM>
I may have missed the point a bit in my response to your question
about:

document.getEle mentById("SomeI D").firstChild. nodeValue = "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">&nb sp;</div>
A general solution is to cleanout the contents, then write your own
textNode:

var el = document.getEle mentById("SomeI D");
while (el.firstChild) {
el.removeChild( el.firstChild);
}
el.appendChild( document.create TextNode('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
9245
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, but there just seems to be no information on the topic that I can find. Basically I need to store a directory structure in memory, from a flat list of files and paths. ie given: C:\dir\dir1\abc.txt C:\dir\toop.txt
1
7184
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 static HTML tree using http://www.treeview.net. now i need to generate this tree dynamically. Can any one has code for this?
3
2163
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 would like to do is to add the new tree branch to the correct place in the document in the browser window using the innerHTML property of the parent node.
4
612
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 to have an API like this: Let Node be a suitably defined data structure. Then the following functions shall be supported:
25
5367
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 to. The parent id of root of the tree is 0. The length of list is not known. What will be the optimal solution? Node* convertToTree(Node* listHead);
3
4444
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 decided that the most effective way would be : When i connect to the server once, I download the list and disconnect, instead of connecting every time i go, to the lower node in my tree hierarhy. Thus i have a problem - how to store data.
3
1479
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 a memory manager , like i) garabage collector kind of thing , ii) Safe Free of pointer in wrapper of Free functions etc . Thanks Uday
9
7831
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 chess position has around 30 moves, it would mean every node of the tree would have 30 branches (on average), which in turn themselves would average about 30 branches each. I can think of a variety of ways of implementing this, including a series...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.