473,903 Members | 3,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=docum ent.createEleme nt("SELECT");
selectBox.name= "theSelectB ox";

optionOne=docum ent.createEleme nt("OPTION");
optionOne.name= "option1";
optionOne.value ="one";
optionOne.text= "one";

selectBox.appen dChild(optionOn e);
--------------

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 3152
Robert Oschler wrote:
optionOne.value ="one";
optionOne.text= "one";

optionOne.setAt tribute('value' ,'one');
textNode = document.create TextNode('one') ;
opionOne.append Child(textNode) ;
selectBox.appen dChild(optionOn e);
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=docum ent.createEleme nt("SELECT");
selectBox.name= "theSelectB ox";

optionOne=docum ent.createEleme nt("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.appen dChild(optionOn e);
--------------

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.c om> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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.appen dChild(optionOn e);
--------------

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.c om> wrote in message
news:43******** *************** @news.euronet.n l...
Robert Oschler wrote:
optionOne.value ="one";
optionOne.text= "one";


optionOne.setAt tribute('value' ,'one');
textNode = document.create TextNode('one') ;
opionOne.append Child(textNode) ;
selectBox.appen dChild(optionOn e);
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.setAt tribute("text", someValue),
still get wiped after the appendChild() call. If I move the appendChild()
call before the (now) newOption.setAt tribute() 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.setAt tribute("text",
someValue), still get wiped after the appendChild() call. If I move
the appendChild() call before the (now) newOption.setAt tribute()
calls, it works fine.


As Peroli already pointed out, the text property doesn't exist on element
creation time. The call setAttribute("t ext", 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.create TextNode 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("t ext", 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.create TextNode 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.c om> wrote in message
news:43******** *************** @news.euronet.n l...
Robert Oschler wrote:
optionOne.va lue="one";
optionOne.te xt="one";


optionOne.set Attribute('valu e','one');
textNode = document.create TextNode('one') ;
opionOne.appe ndChild(textNod e);
selectBox.app endChild(option One);


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.appen dChild( 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
2750
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----- <?xml version="1.0" standalone="no" ?> <!DOCTYPE graph SYSTEM "graph.dtd"> <graph/> --------------
1
2127
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 options are invisible, yet the box seems fully functional. I went into the debugger and sure enough the selection box has "OPTION" child nodes with the correct "value" and "text" attributes. What could be wrong?:
4
4922
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 meant to be rolled to a production environment. What it does: After you displaying the debug window (see tip 1) you can move your mouse over any element on the page and the window will populate with all the properties, events, and objects (yea,...
11
2094
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 "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
27
4641
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 = $doc->loadHTMLFile("./aBasicSearchResult.html"); if ( $res == true ) { $zip = $doc->getElementById('zipRaw_id')->value; if ( 0 != $zip ) {
3
2304
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, see below) .. The code I use to do that is shown below. The code runs fine in FireFox. I can see the newly created table rows and everything looks fine on the page. In Internet Explorer, I don't see any changes to the page after the new nodes...
14
2012
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 just trying to do this in phases to understand what's going on. I'm getting an error (Object doesn't support this property or method) in IE and I can't figure out what I'm doing wrong. Can anyone tell me? if (navigator.appName == "Microsoft...
0
5586
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
15
1414
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, could this error in a standards-compliant browser? <p id="foo">foo</p> <script type="text/javascript>
0
9999
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
9847
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
11282
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
10873
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...
1
10983
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9684
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8048
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7206
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();...
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.