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

another wacky IE problem

i've got a form element (named 'elemA') created in html.

dynamically, in javascript, i duplicate this
element and rename the new element 'elemB'

var elem_a = ...
var elem_b = elem_a.createNode(true);
elem_b.name = 'elemB';

i then add this to the document.

when i print out document.forms[0] array, i can
see the new element listed.

but when i do

document.forms[0]['elemB'] does not find it.

whereas

document.forms[0]['elemA'] works as expected

anyone know why

document.forms[0]['elemB'] does not work

Aug 29 '06 #1
8 1346
Where did you add elemB? Did you append it to the form or to the
document?

Aug 29 '06 #2

"Tom Cole" <tc****@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Where did you add elemB? Did you append it to the form or to the
document?
i added it to the document.

but when i print out the elements of document.forms[0] ...it's in there.

it's when i try to retrieve it by

documents.forms[0]['elemb'] that i get undefined.

thx
Aug 29 '06 #3

"ppcguy" <go*****@hehe.comwrote in message
news:ju*******************@news-wrt-01.rdc-nyc.rr.com...
>
"Tom Cole" <tc****@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Where did you add elemB? Did you append it to the form or to the
document?

i added it to the document.

but when i print out the elements of document.forms[0] ...it's in there.

it's when i try to retrieve it by

documents.forms[0]['elemb'] that i get undefined.

thx

seems like the new element is part of the form - i can see it
visually and have iterated in a loop and printed the name.

it's almost as if

document.forms[0]['elemb']
or
document.forms[0].elements['elemb']

uses a key to find the entry and the key is not set for the
new element.

i added the new element to the document via

document.insertChild()

and as i wrote before, seems to have also added the new element
to the form.

is there a function i have to call, so that
document.forms[0].elements['elemb']
works.
Aug 29 '06 #4

ppcguy wrote:
i've got a form element (named 'elemA') created in html.

dynamically, in javascript, i duplicate this
element and rename the new element 'elemB'

var elem_a = ...
var elem_b = elem_a.createNode(true);
I think you want cloneNode:

var elem_b = elem_a.cloneNode(true);
There is no "createNode" method in the W3C DOM - have you defined it
elsewhere?

elem_b.name = 'elemB';

i then add this to the document.

when i print out document.forms[0] array, i can
see the new element listed.
document.forms is not an array, it's a node collection. How are you
printing it out? You may have added an "elem_b" property to the
document object, not a DOM element that you think you created.
--
Rob

Aug 30 '06 #5

"RobG" <rg***@iinet.net.auwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
I think you want cloneNode:

var elem_b = elem_a.cloneNode(true);

my bad..a typo here...it was a cloneNode()

There is no "createNode" method in the W3C DOM - have you defined it
elsewhere?

elem_b.name = 'elemB';

i then add this to the document.

when i print out document.forms[0] array, i can
see the new element listed.

document.forms is not an array, it's a node collection. How are you
printing it out? You may have added an "elem_b" property to the
document object, not a DOM element that you think you created.
i'm printing it out this way:

var msg = "";
for ( var x = 0; x < document.forms[0].elements.length; ++x )
msg += document.forms[0].elements[x].name + ",";
alert(msg);

when i print it (as above), i see the new name.

i think the dom element is created ok because i see a whole row
of new form elements.

but when i try to retrieve an object...not there...all below fail

alert(document.forms[0].elem_b);
alert(document.forms[0]["elem_b"]);
alert(document.forms[0].elements["elem_b"]);

---

i've got a row of form elements.
i did a cloneNode(true) on the table row.
i iterate thru all the new form objects and rename them
node.name = new_name;
then i inserted the new row just before the last row.

---

the page looks right with the new row inserted in the right posn.

when i print the forms array elements (above), the new names are there.

but when i retrieve individual objects, it fails.


Aug 30 '06 #6
here's the full relevent code

function addRows()
{
var table = document.all('main');

var elem = document.createElement("input");
elem.setAttribute( "type", "text");
elem.setAttribute( "name", "elem_b");

table.appendChild( elem );

var msg = "";
for ( var x = 0; x < document.forms[0].elements.length; ++x )
msg += document.forms[0].elements[x].name + ",";

alert(msg); // prints the name ok

alert(document.forms[0].elem_b); // fails
alert(document.forms[0].elements["elem_b"]); // fails
}

Aug 30 '06 #7

jman wrote:
here's the full relevent code

function addRows()
{
var table = document.all('main');
document.all is all but extinct around here, use feature detection and
getElementById. There are various strategies for including support for
ancient IE versions that need document.all, a simple strategy that
provides limited emulation is:

if ( 'function' != typeof document.getElementById
&& 'function' == typeof document.all )
document.getElementById = document.all;
}

var elem = document.createElement("input");
elem.setAttribute( "type", "text");
elem.setAttribute( "name", "elem_b");
setAttribute is known to be buggy in IE, just don't use it if there is
any alternative. Use dot property access wherever possible (less
typing too :-) ):

elem.type = "text";
elem.name = "elem_b";
Some might say that since the default type for an input is text,
setting its type attribute to 'text' is redundant. I think you're
better off to set it explicitly.

table.appendChild( elem );
Is table an HTML table element?

Note that the *only* element that can be a child of a table is a table
section element (thead, tfoot or tbody). If you haven't coded a tbody
in the source HTML, one will be added by the browser when it loads the
page - it's a mandatory element, although the tags are optional.

Some browsers (but not IE) let you add TR elements to a table, but they
will actually add them to a tbody section. Adding an input element to
a table will result in invalid HTML - error correction may take over
and put it somewhere else that is valid.

var msg = "";
for ( var x = 0; x < document.forms[0].elements.length; ++x )
msg += document.forms[0].elements[x].name + ",";

alert(msg); // prints the name ok

alert(document.forms[0].elem_b); // fails
alert(document.forms[0].elements["elem_b"]); // fails
}
That doesn't look much like the code you originally posted. Here is a
trivial example, tested in IE and Firefox:

<script type="text/javascript">

function addRow(id){
var table = document.getElementById(id);
var rowToClone = table.rows[0];
var oRow = rowToClone.cloneNode(true);
var input = oRow.getElementsByTagName('input')[0];
input.name = 'input' + table.rows.length;
input.value = '';
rowToClone.parentNode.appendChild(oRow);
}

</script>

<button onclick="addRow('tableA')">Add row</button>
<form action="">
<table id="tableA">
<tr>
<td><input name="input0" onclick="this.value=this.name;"></td>
</tr>
</table>
</form>

Aug 31 '06 #8

RobG wrote:
var input = oRow.getElementsByTagName('input')[0];
name = 'input' + table.rows.length;
input.name = name;
input.value = '';
rowToClone.parentNode.appendChild(oRow);
once added, can you retrieve the object via

alert(document.forms[0].elements[name]);

i think i read somewhere that there is an ie bug.

ie does not add it to the form collection.

thx

Sep 1 '06 #9

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

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
17
by: Bruno | last post by:
I have a feature that is hosted on a different domain from the primary one in a frame, and need to retain values in a cookie. example: A web page at one.com contains a frame which has a page...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
5
by: felipevaldez | last post by:
how can I put an element below another element, that's not absolutely positioned? so I have a n element, called X, and next to that, an element Y X Y XXXXXX
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
11
by: Jan | last post by:
Hi: Here's a problem I've had for a long time. The client is really running out of patience, and I have no answers. Access2003, front- and back-end. Single form with 4 subforms (each...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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...

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.