Stefan Mueller wrote:
I tried
xelement = document.createElement("input")
xelement.type = "button"
xelement.name = "MyButton"
but it does not work with the Internet Explorer . I don't
get an error message but if I try to address this button by its name
e.g. document.getElementsByName("MyButton")[0].style.visibility = "hidden"
I get with the Internet Explorer (Mozilla works fine) the following error
message:
Error: 'document.getElementsByName(...).0.style' is null or not an object
This is a known and nasty problem with IE which is documented on MSDN,
for some reasons their implementation is not able to properly set the
name on element objects created the standard way
document.createElement('tagname')
If it is a button it is usually easy to simply do
xelement.id = xelement.name = 'MyButton'
and then that way you can access the button with document.getElementById
the same way other elements can be accessed by its id. Only make sure
the id is unique in the document. Of course the id does not need to be
the same as the name.
If you really need to set the name to access the element by its name
then there are various workarounds, MSDN
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp>
suggests to use the non standard and IE specific
var input = document.createElement(
'<input type="radio" name="radioName">');
This works in IE/Win (I think IE/Mac does not like that) and creates the
element object with proper type and name but that argument with some
markup in the call to createElement will give you lots of problems in
other implementations as some implementations will throw an execption,
others will return null, so it is a pain then to write script to deal
with all implementations.
Doing
var dummyDiv = document.createElement('div');
dummyDiv.innerHTML = '<input type="radio" name="radioName">';
var input = dummyDiv.firstChild;
is sort of a hack too but as far as I am aware does not need browser
specific code forks as IE 5 and onwards, Mozilla 1.0 and later, Netscape
6 and later, Opera 7 and later support that in HTML documents.
--
Martin Honnen
http://JavaScript.FAQTs.com/