IE cannot set 'name' attribute on runtime elements created by createElement

W

Walton

checkout: (3rd paragraph under 'Remarks' section)
http://msdn.microsoft.com/library/d.../author/dhtml/reference/properties/name_2.asp

from the page:
"The NAME attribute cannot be set at run time on elements dynamically
created with the createElement method. To create an element with a
name attribute, include the attribute and value when using the
createElement method."

to work around this you have to code:

document.createElement('<div name="ieiscrap">')

which -surprise- fails in Firefox. i haven't tested in any other
browsers. this one really caused me a headache.

does anyone know why IE does this?
 
L

-Lost

Walton said:
checkout: (3rd paragraph under 'Remarks' section)
http://msdn.microsoft.com/library/d.../author/dhtml/reference/properties/name_2.asp

from the page:
"The NAME attribute cannot be set at run time on elements dynamically
created with the createElement method. To create an element with a
name attribute, include the attribute and value when using the
createElement method."

to work around this you have to code:

document.createElement('<div name="ieiscrap">')

which -surprise- fails in Firefox. i haven't tested in any other
browsers. this one really caused me a headache.

does anyone know why IE does this?

window.onload = function()
{
var p = document.createElement('p');
p.appendChild(document.createTextNode('IE Sucks!'));
p.name = 'IE';
document.getElementsByTagName('body')[0].appendChild(p);
}

<p onclick="alert(document.getElementsByTagName('p')[1].name);">show me
the money, er... name</p>

Works in Internet Explorer 6 (and any DOM-compliant/aware browser or
viewing device).

Does this not work in Internet Explorer 7? If so, they regressed.
 
R

RobG

Walton said:
checkout: (3rd paragraph under 'Remarks' section)
http://msdn.microsoft.com/library/d.../author/dhtml/reference/properties/name_2.asp

from the page:
"The NAME attribute cannot be set at run time on elements dynamically
created with the createElement method. To create an element with a
name attribute, include the attribute and value when using the
createElement method."

to work around this you have to code:

document.createElement('<div name="ieiscrap">')

which -surprise- fails in Firefox. i haven't tested in any other
browsers. this one really caused me a headache.

does anyone know why IE does this?

The key is in the paragraph above the one you referenced:

"Microsoft JScript allows the name to be changed at run time. This does
not cause the name in the programming model to change in the collection
of elements, but it does change the name used for submitting elements."

In other words, IE assigns a "programming model" name attribute when the
control is created and it can't be changed. However, controls seem to
also have a user definable name: if you change the name of the control
programmatically, it is this name that changes, not the "programming
model" name. If you submit the control with the form, the name that is
submitted is the programming model name or the (hidden) user-definable
name if it's been set. The name you set can't be used with the elements
collection, like:

document.form.nameIset

or

document.form.elements['nameIset']


Why is IE like that? Who knows, MS have only had 10 years to fix it,
maybe they like it like it is.
 
W

Walton

yeah, i was using ie7. didn't test in ie6

I should also be a bit clearer with my example. I was creating form
inputs and selects when running into this issue.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top