473,508 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

test overwrite of getElementsByName for IE

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
2 2671
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3674
by: simon | last post by:
<html> <head> <script language="vbscript"> sub window_onload msgbox document.getElementsByName("name1").length end sub </script> </head> <body> <div name="name1">testDiv</div>
21
11337
by: briggs | last post by:
<html> <head> <script> /* AddChild */ function ac() { var c; var p = document.getElementById("p"); for (var i = 0; i < 5; i++) { c = document.createElement("DIV"); // Create 'div' element.
1
9720
by: acord | last post by:
Hi, I want to use document.getElementsByName('name').value to retrieve a value of the field 'name', but it returns undefined. But function document.xxxform.name.value returns the correct value....
2
5658
by: André Wagner | last post by:
I'm trying to get all the "divs" that have a given NAME using getElementsByName(). For example, the following code: <html> <head> <script type="text/javascript"> function on_load() { var...
22
7915
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
4
7957
by: david.kuczek | last post by:
I got the following error in a javascript I wrote. The script works fine, but why is the error being displayed??? ##### Here comes the little script: function...
4
21542
by: Otto Wyss | last post by:
I've tried to access the value of a selected option through the following getSelectValue function, passing the name of the select function getSelectValue (name) { var sel =...
14
8116
rrocket
by: rrocket | last post by:
I am passing a value through query string to a popup and then trying to pass it back to the main page... I store the value in a hidden input box <input type="text" name="FromZip" id="FromZip"...
7
374
by: lukaszmn | last post by:
Hi, I cannot figure out why everything from line "var elem = doc.getElementsByName('keywords')" in x.js is not executed. I want to set value of <input name=keywords ...which is in a.html after page...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.