test overwrite of getElementsByName for IE

T

tsalm

Hi everybody,

I need to get a element create dynamically by its name. It's ok for
FF, but on IE, getElementsByName return "undefined".
I create this code but not sure that it can work on older IE ....if
someone can test this code on older IE ?
( and in the hope that it can be usefull for others ! )

1- It first create a combobox dynamically and set the NAME attribute
to it.
2- It overwrite the getElementsByName method if the nav is IE
3- If all is ok, it must write the innerHTML code in an alert box

thanks for help,
TSalm

=====Test.html====

<html>
<head></head>
<body></body>
<script language="javascript">

// <<< BEGIN INSERT

var values = new Array("a","b","c");

var inputElementName = "SELECT";
var input = document.createElement(inputElementName);

input.setAttribute('name', "test.test[0]");

for (i=0;i<values.length; i++) {
var option = document.createElement("OPTION");
option.setAttribute("value", values);
option.innerHTML = values;
input.appendChild(option);
}

document.body.appendChild(input);
// END >>>

// <<<
function findElByName(nd,strName) {

for (i=0;i<nd.childNodes.length;++i) {

// find it ?
if (nd.childNodes.name==strName) {
return nd.childNodes;
}

// childNode ?
if ((nd.childNodes.hasChildNodes()) && (nd.childNodes.tagName !
= "OPTION")) {
val = findEl(nd.childNodes,strName);
if (val) return val;
}
}

return null;

}

if (navigator.appName == "Microsoft Internet Explorer") {
document.getElementsByName = function(str) {
o = findElByName(document.body,str);
if (o) {
return new Array(o);
}
return undefined;
}
}
// >>>


alert(document.getElementsByName("test.test[0]")[0].innerHTML);

</script>
</html>

====END====
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top