Just for a change... this doesn't work in IE .. but does in Mozilla!

W

webdev

Hi,

Code below dynamically adds an input box to a form, creating a new name
attribute as it goes... (many thanks to Martin Honnen for getting me this
far ;0) Problem is - in IE5.5 only the first form field (the hard-coded one)
is recovered from the form, while with Mozilla/Firefox all fields are.

The alert() just shows that the newly created element has no name in IE. Can
anyone offer help with this bizarre problem (I thought any old rubbish would
run in IE - even mine!)

Regards
Rae MacLeman
----------------------------------------------------------------------------
---------

<HTML>
<HEAD>
</HEAD>
<SCRIPT LANG=Javascript>
<!--
var gCount=1;
function DoChange() {
var objNewName, objBR;
gCount++;
objNewName = document.createElement('INPUT');
objNewName.setAttribute('NAME', 'fname'+gCount);
alert('object name = '+objNewName.name);
objNewName.setAttribute('ID', 'name'+gCount);
objBR = document.createElement('BR');
document.getElementById("names").appendChild(objNewName);
document.getElementById("names").appendChild(objBR);
delete objNewName;
delete objBR;
}
//-->
</SCRIPT>

<BODY>
<form action="mozilla_test2_handler.asp" method="POST">
<div id="names" name="names">
<input id="fname1" name="fname1" type="text"><br>
</div>
<INPUT TYPE=button ONCLICK=DoChange() VALUE='Add name'><input
type="submit"></form>
</BODY>
</HTML>
 
M

Michael Winter

[snip]

Valid documents require a TITLE element to be present.
</HEAD>
<SCRIPT LANG=Javascript>

There is no such thing as a lang attribute. You intended language, but
even that should be avoided as it has been deprecated in favour of the
(required) type attribute.


Hiding scripts is an obsolete practice. All browsers in use understand the
SCRIPT element, even if they can't execute scripts.
var gCount=1;
function DoChange() {
var objNewName, objBR;
gCount++;
objNewName = document.createElement('INPUT');
objNewName.setAttribute('NAME', 'fname'+gCount);
alert('object name = '+objNewName.name);
objNewName.setAttribute('ID', 'name'+gCount);

Don't use the setAttribute method with HTML documents. Instead, use the
relevant properties.

objNewName.name = 'fname' + gCount;
objNewName.id = 'name' + gCount;
objBR = document.createElement('BR');
document.getElementById("names").appendChild(objNewName);
document.getElementById("names").appendChild(objBR);

Instead of calling getElementById twice, consider saving the reference
returned by the first call and use it twice.
delete objNewName;
delete objBR;

You cannot delete local variables. It won't cause an error, but it doesn't
do anything, either. You can see this by looking at the return value of
the delete operator. If the property being deleted doesn't exist, or it
does and has been deleted, the delete operator returns true. If the
property cannot be deleted, the operator returns false.

// In a function
var test = {};
alert(test); // alerts [object Object]
alert(delete test); // alerts false
alert(test); // alerts [object Object]

Even if you could delete a local variable, there's no point. When the
function returns, local variables are destroyed[1] and any objects will
have their reference counts decremented.

[snip]
<div id="names" name="names">

DIV elements do not have a name attribute.

[snip]
<INPUT TYPE=button ONCLICK=DoChange() VALUE='Add name'>

You need to quote that onclick attribute value.

... onclick="DoChange()" ...

[snip]

Hope that helps,
Mike


[1] Obviously, a closure will stop this, but that doesn't occur here. If
it did, assign null to the variables to explicitly remove the references.
 
M

Michael Winter

[snip]
<SCRIPT LANG=Javascript>

There is no such thing as a lang attribute.

Before anyone comments on that potentially misleading statement, I'll
rephrase it.

The lang attribute does exist as part of the internationalisation (i18n)
attribute set, however it is not applicable to SCRIPT elements. It
specifies a two character language code[1], along with another optional
subset code separated by a dash (-), specifying the language used as
content in the relevant element.

For example, to indicate that an entire document is written in English,
you can specify:

<html lang="en">

Whereas

<img lang="fr" alt="Voici Fabienne, mon &eacute;pouse."
src="fabienne.jpg">

indicates that the alt text is French.

[snip]

Mike


[1] Language codes specified in ISO 639, and the accompanying subset
codes, are usually two characters. However, they can be anything from one
to eight characters in length. Some registered examples of this include
i-klingon and en-scouse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top