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

Home Posts Topics Members FAQ

innerHTML / Appendchild question

Hi folks;

I have this webpage, allowing users to insert multiple records in one post
into the database.

Because I don't know the exact number of records they want to add, I created
a little javascript
that adds a new line with HTML inputboxes and lists to the form if they
press a button.

My first version of the script used innerHTML to add the new line of HTML
It works great in IE. But in NS/Mozilla each time I add new items the
already existing textboxes
(filled in) and already existing listboxes (with selected items) 'loose'
their data or selections.

var newhtmltoadd = '<input type=....' // The new line of html to add.
document.newlin e.innerHTML = document.newlin e.innerHTML + newhtmltoadd;

So I figured out this innerHTML way might not be the most fancy solution to
my problem and
I tried by the using the more DOM approach : appendChild

But the problem remains the same. Works fine in IE, NS/Mozilla reinitialise
my exisiting form objects in the area where I append.

Here is my appendChild code

var listbox=documen t.createElement ('select');
listbox.setAttr ibute("name","F kRelie-New-"+id);
listbox.setAttr ibute("class"," defaultlistbox" );

//fill listbox with data from similar existing listbox

for(var
i=0;i<document. getElementById( 'FkRelie-New-1').length;i++) {
listbox.options[i]=new
Option(document .getElementById ("FkRelie-New-1").options[i].text);
listbox.options[i].value=new
Option(document .getElementById ("FkRelie-New-1").options[i].value);
}

document.getEle mentById('NewPt Left').appendCh ild(listbox);

Now. Is this a NS/Mozilla problem or am I doing something wrong?
How can I preserve state from my already exising form objects when adding
new ones in NS/Mozilla?

Thanx in advance,

Nicolas
Jul 23 '05 #1
1 2787
Nicolas Van Lancker wrote:
<snip>
Here is my appendChild code

var listbox=documen t.createElement ('select');
listbox.setAttr ibute("name","F kRelie-New-"+id); ^^^^ ^^^^^^^^^^^^
The implication of this attribute creation is that the existing SELECT
elements also have name attributes taking the form - FkRelie-New-1 -,
but the fact that you are not setting an ID attribute at this point
suggest that the existing SELECT elements do not have corresponding ID
attributes.

Incidentally, using - setAttribute - is generally less successful than
assigning values directly to the corresponding W3C HTML/Core DOM
specified object properties.

listbox.setAttr ibute("class"," defaultlistbox" );

//fill listbox with data from similar existing listbox

for(var
i=0;i<document. getElementById( 'FkRelie-New-1').length;i++) { ^^
But here you are using the - getElementById - method to acquire a
reference to an existing select element. In a normal context (no custom
DTD) the "Id" at the end of - getElementById - means what it says; only
elements with ID attributes should be expected to be retrievable using
that method. The IE implementation of - getElementById - is buggy and
will return references to elements that only have corresponding NAME
attributes (because it is optimised to make the reference in a
collection that contains properties corresponding with the NAME
attributes of elements in addition to ID attributes) but no other
browsers are known to exhibit that flaw.

Generally, the retrieval of references to form control within HTML pages
is better done using the W3C HTML DOM specified - document.forms - and -
elements - collections, which make controls with NAME attributes
directly available (along with elements that have ID attributes) as
named properties.
listbox.options[i]=new
Option(document .getElementById ("FkRelie-New-1").options[i].text);
listbox.options[i].value=new
Option(document .getElementById ("FkRelie-New-1").options[i].value);
}
Now that is a little odd; you create and assign an Option element and
then immediately create a second Option element and use it to replace
the first.

The repeated element retrieval for the same element is extremely
inefficient, it would make more sense to retrieve the reference to the
element once and assign it to a local variable. Which would also allow
you to verify that the retrieval attempt was successful before acting on
what you are otherwise assuming is an object (but may be null under some
circumstances).

<snip> Is this a NS/Mozilla problem or am I doing something wrong?

<snip>

Mozilla, and all other Gecko based browsers, are extremely good at what
you are attempting to do here (better than IE in some respects), so if
it isn't working it is going to be your coding that is at fault.

Richard.
Jul 23 '05 #2

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

Similar topics

22
2864
by: necromonger | last post by:
Hi, I've got this code that creates a new new row and cell. I then put some text into the cell with innerHTML - works beautifully with Firefox but fails with IE. I guess IE doesn't support this way of doing it, but is there another way of doing it with DOM? newr = document.createElement('tr'); stbl.appendChild(newr);
1
1326
by: jhcorey | last post by:
I am doing some maintenance on a js tree control that was done with proprietary IE code. When I click on a node it calls a function which has code similar to that below to expand the tree. "el" refers to the div that I clicked on. var workHtml = el.innerHTML; for (i = 0; i < idx; i++)
4
6553
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using createElement methods, then added a bit of extra text using innerHTML, only to find most of the attributes removed from the table. Below is a script that calls the same code to add a table inside a div. It adds an onclick to the div and...
2
9137
by: writeOnceDebugEverywhere() | last post by:
- I believe it is a simple question: my onclick event for the button looks as follows: ...... // reference to the <div id="messages" : var mDiv=document.getElementById('messages'); mDiv.innerHTML += 'bla.. bla.. bla'; mDiv.innerHTML += '<br/>'; - at the some point, when <div> area becomes full, I want somehow to
10
12684
by: Jake Barnes | last post by:
This weekend I wanted to learn AJAX, so I set up a little toy page where I could experiment. The idea of this page is that you click in one of the boxes to get some controls, at which point you can add text, images, or HTML to the box. This seems to work fine in FireFox, but not in IE. You can see the problem here: http://www.publicdomainsoftware.org/ajaxExperiment.htm In FireFox, if you click in a box and then select "Add HTML" you...
6
3070
by: CES | last post by:
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.nodeValue = "XXX"; Thanks in advance. - CES
6
2997
by: sonic | last post by:
Ok, i am sure everyone is sick of hearing about this. but i've checked about 10 different posts/sites about this issue, and they all say "use DOM" but i think there is more to be said. Perhaps I am a total newbie but the answer was not immediately obvious to me here. so.. problem: declaring doctype as xhtml will prevent myDiv.innerHtml=val from working. suggested solution:
9
3399
by: martymix | last post by:
simple question: I have a simple <dt>test text</dt> I get the innerHTML of that dt, and I try and append some text to it like so: dt = document.getElementsByTagName('dt') var text = dt.innerHTML + 'new' in Firefox, I get "test textnew"
4
19575
by: Dan Andrews | last post by:
Hello, I was wondering what is the correct way to handle special characters via javascript and the DOM. I would like to avoid document.write and innerHTML. What I am doing is dynamically creating options for a select. The innerHTML example below works for firefox and internet explorer, but is this the accepted way of dynamically adding special characters. option = document.createElement("OPTION");
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
10324
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
10147
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
10090
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
9949
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
6739
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
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
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.