By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,611 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,611 IT Pros & Developers. It's quick & easy.

test overwrite of getElementsByName for IE

P: n/a
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[i]);
option.innerHTML = values[i];
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[i].name==strName) {
return nd.childNodes[i];
}

// childNode ?
if ((nd.childNodes[i].hasChildNodes()) && (nd.childNodes[i].tagName !
= "OPTION")) {
val = findEl(nd.childNodes[i],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====

Feb 24 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Nobody have IE 5 ?
On 24 fév, 01:13, "tsalm" <t...@free.frwrote:
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[i]);
option.innerHTML = values[i];
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[i].name==strName) {
return nd.childNodes[i];
}

// childNode ?
if ((nd.childNodes[i].hasChildNodes()) && (nd.childNodes[i].tagName !
= "OPTION")) {
val = findEl(nd.childNodes[i],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====

Feb 28 '07 #2

P: n/a
tsalm said the following on 2/28/2007 9:58 AM:
Nobody have IE 5 ?
Are you kidding? Trying to find an IE5.5 user is hard enough, much less
an IE5 user.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.