I think the for (var i in ...) is the problem. My guess is i is not an integer, but an object (element).
I think you should store the document.getElementsByClassName('someclass') in an array and loop through it using for (i=0; i
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
ZVN,
As Pritaes said plus ......
Unfortunately, .getElementsByClassName is far from safe. Too many browsers don't support it. So you have to be a bit old fashioned and do it longhand with .getElementsByTagName then test for .className in the loop.
Try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Airshow :: Untitled</title>
<style type="text/css">
{}
</style>
<script>
onload = function(){
var elArray = document.getElementsByTagName('span');
for(var i=0; i<elArray.length; i++){
if(elArray[i].className !== 'someclass') continue;
var elArray2 = elArray[i].getElementsByTagName('a');
for(var j=0; j<elArray2.length; j++){
elArray2[j].onclick = function(){
return alert(this.innerHTML + ' : ' + this.href);
return false;
};
}
}
}
</script>
</head>
<body>
<ul class="x">
<li class="a"><span class="someclass"><a href="http://someurl/x1/A">link text 1</a></span></li>
<li class="b"><span class="someclass"><a href="http://someurl/x1/B">link text 2</a></span></li>
</ul>
<ul class="y">
<li class="a"><span class="someclass"><a href="http://someurl/x1/C">link text 3</a></span>
</ul>
<ul class="z">
<li class="a"><span class="someclass"><a href="http://someurl/x1/D">link text 4</a></span>
</ul>
</body>
</html>
As you see, I have used !Doctype XHTML but I think it should work for XML too.Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Thanks for the example Airshow. Just one more reason to use jQuery for me ;)
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
jQuery, Prototype etc. are good but tricky. I encourage novices to learn JS basics before trying to use these libs. People can't run before they can walk.
My rules (only occasionally broken) : if it's a jQuery (etc.) question then it gets a jQuery answer; otherwise basic JS. I might steer someone in the direction of jQuery if I think the problem is large enough to warrant the overhead and the poster appears advanced enough to cope with it.
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372