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

DOM: Properties set before calling appendChild() are lost after call

I'm having a very painful time converting some Mozilla dynamic DOM code to
work with Internet Explore. For example, given this code:

--------------
selectBox=document.createElement("SELECT");
selectBox.name="theSelectBox";

optionOne=document.createElement("OPTION");
optionOne.name="option1";
optionOne.value="one";
optionOne.text="one";

selectBox.appendChild(optionOne);
--------------

This code doesn't work because the option element text property becomes
empty after I execute the appendChild() method. HOWEVER, if I put the
appendChild() call BEFORE I set the "text" property all is well.

This also seems to be the case for several other properties too.

The problem is, a lot of my code was structured around having several child
nodes pre-created, and THEN adding them to the container/parent node. This
worked fine in Mozilla, but because of the above mentioned "quirk", fails
miserably with IE. Annoyingly enough, with IE you HAVE to set the type
before calling appendChild() or IE will throw an error when you try to set
it after the appendChild() call (the type property is write-once only and
apparently calling appendChild() affects the "type" property in some way).

Before I rewrite a whole bunch of code, is there a workaround or a known
technique for dealing with this?

If I am wrong about this, then tell me why I lose the values of certain
properties after I call appendChild()?

Thanks
Sep 3 '05 #1
7 3109
Robert Oschler wrote:
optionOne.value="one";
optionOne.text="one";

optionOne.setAttribute('value','one');
textNode = document.createTextNode('one');
opionOne.appendChild(textNode);
selectBox.appendChild(optionOne);
If I am wrong about this, then tell me why I lose the values of
certain properties after I call appendChild()?


Restrict yourself to use DOM setters only and you will be fine.
JW

Sep 3 '05 #2
hi,
Robert Oschler wrote:
I'm having a very painful time converting some Mozilla dynamic DOM code to
work with Internet Explore. For example, given this code:

--------------
selectBox=document.createElement("SELECT");
selectBox.name="theSelectBox";

optionOne=document.createElement("OPTION");
optionOne.name="option1";
optionOne.value="one";
optionOne.text="one";
This will work only if "optionOne" is in the HTML DOM. Since you
haven't added it to the DOM yet, you can set its attribute only with
"setAttribute()" method of DOM.


selectBox.appendChild(optionOne);
--------------

This code doesn't work because the option element text property becomes
empty after I execute the appendChild() method. HOWEVER, if I put the
appendChild() call BEFORE I set the "text" property all is well.

See, that answers them all.
This also seems to be the case for several other properties too.
Yes. Right.

The problem is, a lot of my code was structured around having several child
nodes pre-created, and THEN adding them to the container/parent node. This
worked fine in Mozilla, but because of the above mentioned "quirk", fails
miserably with IE. Annoyingly enough, with IE you HAVE to set the type
before calling appendChild() or IE will throw an error when you try to set
it after the appendChild() call (the type property is write-once only and
apparently calling appendChild() affects the "type" property in some way).

Always follow W3C DOM Convention in accessing DOM Elements. You will be
fine.
Before I rewrite a whole bunch of code, is there a workaround or a known
technique for dealing with this?

If I am wrong about this, then tell me why I lose the values of certain
properties after I call appendChild()?

Thanks


- Peroli Sivaprakasam

Sep 3 '05 #3

"Peroli" <pe****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hi,

This will work only if "optionOne" is in the HTML DOM. Since you
haven't added it to the DOM yet, you can set its attribute only with
"setAttribute()" method of DOM.


selectBox.appendChild(optionOne);
--------------

See, that answers them all.
This also seems to be the case for several other properties too.


Yes. Right.

Always follow W3C DOM Convention in accessing DOM Elements. You will be
fine.


Peroli,

See my reply to Janwillem.

Thanks.
Sep 3 '05 #4

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:43***********************@news.euronet.nl...
Robert Oschler wrote:
optionOne.value="one";
optionOne.text="one";


optionOne.setAttribute('value','one');
textNode = document.createTextNode('one');
opionOne.appendChild(textNode);
selectBox.appendChild(optionOne);
If I am wrong about this, then tell me why I lose the values of
certain properties after I call appendChild()?


Restrict yourself to use DOM setters only and you will be fine.
JW


Janwillem,

Just tried that, no luck. The "text" property of new "OPTION" nodes added
to the selection box, even using newOption.setAttribute("text", someValue),
still get wiped after the appendChild() call. If I move the appendChild()
call before the (now) newOption.setAttribute() calls, it works fine.

Thanks.
Sep 3 '05 #5
Robert Oschler wrote:
Just tried that, no luck. The "text" property of new "OPTION" nodes
added to the selection box, even using newOption.setAttribute("text",
someValue), still get wiped after the appendChild() call. If I move
the appendChild() call before the (now) newOption.setAttribute()
calls, it works fine.


As Peroli already pointed out, the text property doesn't exist on element
creation time. The call setAttribute("text", someValue) just creates a text
attribute, while you are simply dealing with the text node which contains
the displayed text for the property.

That's why you will have to use document.createTextNode to create the text
node and append it to the option element as I pointed out before.
JW

Sep 3 '05 #6
On 03/09/2005 21:02, Janwillem Borleffs wrote:
As Peroli already pointed out, the text property doesn't exist on element
creation time.
That reasoning is somewhat dubious as this behaviour is specific to use
of the text property and the appendChild method in IE. That is, if you
add the OPTION element using the add method, it is added correctly.
Moreover, other browsers do not have issues with the former approach.
The call setAttribute("text", someValue) just creates a text
attribute, [...]
Which is why using the setAttribute method is not appropriate; not all
shortcut properties are directly related to element attributes, and this
is a very clear example.
That's why you will have to use document.createTextNode to create the text
node and append it to the option element as I pointed out before.


For the record, I'm not disagreeing with this approach as they are
equivalent in the end (a text node child will be created with the value
assigned to the text property). What I am doing is correcting what you
are stating as the problem: inconsistent behaviour in IE using the
appendChild child method.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 4 '05 #7
Robert Oschler wrote:
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:43***********************@news.euronet.nl...
Robert Oschler wrote:
optionOne.value="one";
optionOne.text="one";


optionOne.setAttribute('value','one');
textNode = document.createTextNode('one');
opionOne.appendChild(textNode);
selectBox.appendChild(optionOne);


You could do it all in one go using new Option which has the following
format:

var opt = new Option( text, value, defaultSelected, currentSelected);

In your case, you'd use:

var opt = new Option( 'one', 'one', false, false );
selectBox.appendChild( opt );

It's not W3C DOM but it works consistently on a wide variety of browsers
(i.e. it works in IE).

More stuff is here:

<URL:http://www.quirksmode.org/js/options.html>
[...]

--
Rob
Sep 4 '05 #8

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

Similar topics

2
by: John VanRyn | last post by:
Ok, I have XML like this (XML Exhibit A) that I read in from a file ala .. $doc->load("./Template.xml"); because I could not figure out how to set the !DOCTYPE element. ------ XML Exhibit A-----...
1
by: Robert Oschler | last post by:
The code below works great in Mozilla. In IE the selection box is created, and there is a drop-down box if I click on the down arrow, but I can't see the OPTION text for each option. The...
4
by: Robert Skidmore | last post by:
I made this javascript to help me debug some javascript one time and I have just been adding to it over the years. It is IE specific (sure it would not be to hard to change that). It is not ever...
11
by: Mellow Crow | last post by:
I had a problem in IE 6 when trying to insert a table using W3C DOM techniques. I found a solution and share it. :) Initially I had...... ********************** <!DOCTYPE html PUBLIC...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
3
by: robert.oschler | last post by:
I have an AJAX driven page where I dynamically add rows to a known table on the page, based on the return document from the AJAX call, using DOM node methods (except for a small snippet of code,...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
15
by: Peter Michaux | last post by:
Hi, In the following snip of and HTML page, is there a specification guarantee that the foo element will be parsed and available as part of the DOM by the time the script executes? That is,...
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...
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.