473,394 Members | 1,701 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,394 software developers and data experts.

Mozilla DOM Element

Hello all,

This is probably an elementary (no pun intended) question, but I've
spent all afternoon on it and it's driving me crazy.

I have a function which dynamically adds to a table. It receives a
variable which basically encapsulates this:

<div id="tableid">
<tr>
<td>col1</td>
<td>col2</td>
...
</tr>
...
</div>

It's worth nothing that the incoming variable is a product of XSLT
transformation, so I think it's technically an XML DOM element
(although I'm not too sure on the difference between the XML DOM and
the HTML DOM).

On the incoming variable, I do getElementsByTagName("tr") and -("td")
to get NodeLists of the rows and columns respectively. To insert them
into the table, I create new tr and td elements, then copy the value
over, like this:

//stuff to get a column
trs = getElementsByTagName("tr");
tds = trs[i].getElementsByTagName("td");
thiscol = tds[j];

//stuff to copy the column value
new_tr = document.createElement("tr");
new_td = document.createElement("td");
new_td.innerHTML = thiscol.xml

The .xml part is a Microsoft creation, so the only works in Internet
Explorer. In anything else the column value is rendered as 'undefined'.

I'm trying to make things work in Mozilla too, but an Element node
(thiscol.nodeType gives me '1') doesn't have nodeValue implemented.
InnerHTML and OuterHTML are not implemented either.

How on earth are you supposed to extract a value from an XML node if
nodeValue is not defined? Am I going about things in the wrong way?

Many thanks in advance,
Matt.

Jul 3 '06 #1
8 2243
be*************@gmail.com wrote:
Hello all,

This is probably an elementary (no pun intended) question, but I've
spent all afternoon on it and it's driving me crazy.

I have a function which dynamically adds to a table. It receives a
variable which basically encapsulates this:

<div id="tableid">
<tr>
tr isn't a child of div, it belongs in a table, under a thead, tbody or
tfoot.
>
//stuff to get a column
trs = getElementsByTagName("tr");
tds = trs[i].getElementsByTagName("td");
thiscol = tds[j];
If you use a table as you should, you can use rows[].cells[].
//stuff to copy the column value
new_tr = document.createElement("tr");
new_td = document.createElement("td");
new_td.innerHTML = thiscol.xml
If you use a table as you should, you can use insertRow() and insertCell().
The .xml part is a Microsoft creation, so the only works in Internet
Explorer. In anything else the column value is rendered as 'undefined'.

I'm trying to make things work in Mozilla too, but an Element node
(thiscol.nodeType gives me '1') doesn't have nodeValue implemented.
InnerHTML and OuterHTML are not implemented either.
Yes, they all are. So what you have isn't an Element.
How on earth are you supposed to extract a value from an XML node if
nodeValue is not defined? Am I going about things in the wrong way?
It is.

--
Ian Collins.
Jul 3 '06 #2
Ian,

Thanks for the fast reply.

I think my problem stems from the fact that the element that contains
the new rows is generated by XSLT - it's XML and not in DOM form.

I've tried to follow your advice, replaced the <divwith a <table>
tag, and treated the incoming element as a table. But I get exceptions
when I call table.rows[i] etc. presumably because the element is not a
table - it's an XML node.

Calling nodeType on the incoming element when it enters the function,
you get 9 (document), and calling it on
incoming_element.documentElement, you get 1 again.

http://mozref.com/reference/objects/Node is the page that has confused
me about nodeValue - "Only Attr, CDATASection, Comment,
ProcessingInstruction, and Text objects can contain a value in this
property". Am I missing something here? I'm pretty confident my
original approach would work, if I could only find a way to see the
value inside the XML node.

Failing that, I'm guessing that I need to do some transformation
between XML and DOM representations. Does anyone have any advice?

Many thanks,
Matt.
Ian Collins wrote:
be*************@gmail.com wrote:
Hello all,

This is probably an elementary (no pun intended) question, but I've
spent all afternoon on it and it's driving me crazy.

I have a function which dynamically adds to a table. It receives a
variable which basically encapsulates this:

<div id="tableid">
<tr>
tr isn't a child of div, it belongs in a table, under a thead, tbody or
tfoot.

//stuff to get a column
trs = getElementsByTagName("tr");
tds = trs[i].getElementsByTagName("td");
thiscol = tds[j];
If you use a table as you should, you can use rows[].cells[].
//stuff to copy the column value
new_tr = document.createElement("tr");
new_td = document.createElement("td");
new_td.innerHTML = thiscol.xml
If you use a table as you should, you can use insertRow() and insertCell().
The .xml part is a Microsoft creation, so the only works in Internet
Explorer. In anything else the column value is rendered as 'undefined'.

I'm trying to make things work in Mozilla too, but an Element node
(thiscol.nodeType gives me '1') doesn't have nodeValue implemented.
InnerHTML and OuterHTML are not implemented either.
Yes, they all are. So what you have isn't an Element.
How on earth are you supposed to extract a value from an XML node if
nodeValue is not defined? Am I going about things in the wrong way?
It is.

--
Ian Collins.
Jul 3 '06 #3
be*************@gmail.com wrote:

Please don't top post, your reply should come after the (trimmed) text
of the message you are replying to, or interleaved with it.
Ian Collins wrote:
>>be*************@gmail.com wrote:
>>>Hello all,

This is probably an elementary (no pun intended) question, but I've
spent all afternoon on it and it's driving me crazy.

I have a function which dynamically adds to a table. It receives a
variable which basically encapsulates this:

<div id="tableid">
<tr>

tr isn't a child of div, it belongs in a table, under a thead, tbody or
tfoot.

>>>//stuff to get a column
trs = getElementsByTagName("tr");
tds = trs[i].getElementsByTagName("td");
thiscol = tds[j];

If you use a table as you should, you can use rows[].cells[].

>>>//stuff to copy the column value
new_tr = document.createElement("tr");
new_td = document.createElement("td");
new_td.innerHTML = thiscol.xml

If you use a table as you should, you can use insertRow() and insertCell().

>>>The .xml part is a Microsoft creation, so the only works in Internet
Explorer. In anything else the column value is rendered as 'undefined'.

I'm trying to make things work in Mozilla too, but an Element node
(thiscol.nodeType gives me '1') doesn't have nodeValue implemented.
InnerHTML and OuterHTML are not implemented either.

Yes, they all are. So what you have isn't an Element.

>>>How on earth are you supposed to extract a value from an XML node if
nodeValue is not defined? Am I going about things in the wrong way?

It is.
Ian,

Thanks for the fast reply.

I think my problem stems from the fact that the element that contains
the new rows is generated by XSLT - it's XML and not in DOM form.
Very likely, so you have an (X)XML document parsed into a vanilla DOM,
not an HTML one.
I've tried to follow your advice, replaced the <divwith a <table>
tag, and treated the incoming element as a table. But I get exceptions
when I call table.rows[i] etc. presumably because the element is not a
table - it's an XML node.
Because you document isn't (X)HTML, you will have to create HTML DOM
elements form it, which is a pain. It might be easier to get your XML
as a string and use innerHTML to add it to your document.
Calling nodeType on the incoming element when it enters the function,
you get 9 (document), and calling it on
incoming_element.documentElement, you get 1 again.
That's correct, 1 is an element node.
http://mozref.com/reference/objects/Node is the page that has confused
me about nodeValue - "Only Attr, CDATASection, Comment,
ProcessingInstruction, and Text objects can contain a value in this
property". Am I missing something here? I'm pretty confident my
original approach would work, if I could only find a way to see the
value inside the XML node.
No, I might have confused you. Only that subset of object have text
content (nodeValue). Everything else has child nodes.
Failing that, I'm guessing that I need to do some transformation
between XML and DOM representations. Does anyone have any advice?
As I said above, you might be better off with text and innerHTML.
Mozilla will let you insert an XML fragment into the current DOM, but IE
will not.

--
Ian Collins.
Jul 3 '06 #4


be*************@gmail.com wrote:

I think my problem stems from the fact that the element that contains
the new rows is generated by XSLT - it's XML and not in DOM form.
But if you have an XSLT stylesheet that generates HTML (e.g. xsl:output
method="html") then Mozilla's XSLT processor creates HTML element nodes
(for elements with HTML tag names like e.g. div, p in no namespace).
With Mozilla's XSLT API you should probably use transformToFragment with
the target document passed in and make sure your stylesheet generates
HTML. Then you can simply take the fragment node returned and insert it
as needed into the HTML document you have. No need to work with innerHTML.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 4 '06 #5
Sorry about the top posting.

Thanks to both of you for the advice, I understand the problem now.

Cheers,
Matt.

Jul 5 '06 #6
be*************@gmail.com wrote:
Sorry about the top posting.

Thanks to both of you for the advice, I understand the problem now.
For future reference, do you have a solution?

--
Ian Collins.
Jul 5 '06 #7

Ian Collins wrote:
For future reference, do you have a solution?
Not quite yet. I'm using Sarissa so I can emulate Mozilla's XSLT API in
internet explorer, so I think Martin's approach will work best for me.

When you use transformToFragment, what can you use as the sourceDoc? I
understand it has to be a document of the same type (ie XML/HTML) that
the fragment is being transformed into.

In this case, it will be an HTML document. Do I have to supply the
containing webpage as the sourceDoc, and if so where does the fragment
get added? Otherwise, how do I create an empty HTML document, and then
insert it into my webpage later?

Many thanks,
Matt.

Jul 6 '06 #8
I have found at least a temporary solution to this problem.

I tried both your approaches, with little success:

Martin - I created an empty DOM document (with Sarissa) and then tried
to transformFragment into it, but it didn't come out as an HTML DOM
Document. Actually nodeType gave 11 (document-fragment). I also tried
using the keyword 'document' as the sourceDoc, but met with another
error so I quit.

Ian - the innerHTML looked like it was going to work, but I couldn't
get the information I needed as a string. The problem is that I was
adding multiple <trelements to my table - and Sarissa's
stringSerializer requires a valid incoming XML file - i.e. a single
root node. Obviously I could have used an arbitrary root node and then
cropped the string, but it wouldn't have been very neat.

Instead, I reverted to my original problem: getting Mozilla to
recognise the xml within a DOM element node. The answer was simple: use
Sarissa's (cross-browser) stringSerializer function to parse the xml
into a string. I then add it to the document using innerHTML.

So there is at least one solution to this problem. My question now is,
is it the most elegant? There's a lot of contention out there about
innerHTML. According to the standards, how *should* I have solved this
problem?

Thanks for all the advice,
Matt.

Jul 8 '06 #9

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

Similar topics

6
by: knocte | last post by:
I come across with this topic because it is more or less related to PHP. Sometimes we implement a PHP script that displays a form which has a SELECT that should have a value by default. If the...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
5
by: Mikko Rantalainen | last post by:
See example at <URL:http://www.cc.jyu.fi/~mira/moz/formtest.php>. The problem is that the label of submit button is always centered on the button regardsless of 'text-align' property in CSS....
7
by: Jonathan | last post by:
Hi I open a new window using a javascript function: function nw(theURL,winName,features) { //v2.0 window.open(theURL,winName,features); } which I call thus:
4
by: Brian Glen Palicia | last post by:
My goal is to accept input from the user into a text box and then parse the data using split(). The first step is this tiny program to test the split() function. It runs in IE, but in Mozilla it...
8
by: Nicolás Lichtmaier | last post by:
Hi, some time ago I've written an article about this issue. It explain some differences in Explorer's and Mozilla's JavaScript/DOM. It has recently changed its URL, This is the new one: ...
1
by: Ryan Stewart | last post by:
If you don't want to read this post because of its length, I understand. I've spent two and a half days on this problem and have a good deal of information to relate. And this is kind of a long...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
15
by: prabhdeep | last post by:
Hi, Can somebody explain, why in following code, i get "event not defined" error funcTest(sMenu) { doument.getElementById('id1').addEventListener('mousedown', function(){ click(sMenu,...
5
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
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...

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.