V
VK
.... or my script, or my mind, or both?
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
function init() {
var arr = document.getElementsByTagName('DIV');
for (i=0; i<arr.length; i++) {
arr.addEventListener(
'click',function(e){myFunction(arr,e);},true);
}
}
function myFunction(obj,evt) {
alert(obj.id);
}
</script>
</head>
<body bgcolor="#FFFFFF" onload="init()">
<div id='d1'>Some <a href="javascript:void(0)" id='s1'>link</a></div>
<div id='d2'>Some <a href="javascript:void(0)" id='s2'>link</a></div>
</body>
</html>
The above simply doesn't work - myFunction gets undefined as obj ! But
with a bogus intermediary var it works like a charm:
function init() {
var arr = document.getElementsByTagName('DIV');
var foo = null;
for (i=0; i<arr.length; i++) {
foo = arr;
arr.addEventListener(
'click',function(e){myFunction(foo,e);},true);
}
}
All the same in IE (with attachEvent). What a hey?
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
function init() {
var arr = document.getElementsByTagName('DIV');
for (i=0; i<arr.length; i++) {
arr.addEventListener(
'click',function(e){myFunction(arr,e);},true);
}
}
function myFunction(obj,evt) {
alert(obj.id);
}
</script>
</head>
<body bgcolor="#FFFFFF" onload="init()">
<div id='d1'>Some <a href="javascript:void(0)" id='s1'>link</a></div>
<div id='d2'>Some <a href="javascript:void(0)" id='s2'>link</a></div>
</body>
</html>
The above simply doesn't work - myFunction gets undefined as obj ! But
with a bogus intermediary var it works like a charm:
function init() {
var arr = document.getElementsByTagName('DIV');
var foo = null;
for (i=0; i<arr.length; i++) {
foo = arr;
arr.addEventListener(
'click',function(e){myFunction(foo,e);},true);
}
}
All the same in IE (with attachEvent). What a hey?