PDA

View Full Version : IE: Loop applies last value to all elements?



JAB Creations
02-07-2010, 12:00 AM
This is an IE specific script issue, no other browsers can see the script (conditional comments). I'm trying to fix a very miscellaneous problem with IE (imagine that?) though in the simplest of terms IE 6/7 is not seeing JavaScript related attributes on elements (e.g. onmouseover, onblur). I corrected a similar issue where IE was no applying CSS class attributes (IE is just outright having issues with attributes).

So any way I'm trying to re-apply the attributes without having to muck up my XHTML code with anything intended for far outdated but people-are-still-using-it browsers. The problem is that somehow (and I've encountered this before though too far back to remember how I got it working) is that the last onmouseover attribute is applied to all of the elements where I find the onmouseover attribute (though IE does not trigger). I can apply this almost as desired only every onmouseover event will only trigger the last script versus their respective script. I'm pretty upset at myself for not remembering how I fixed this in the past. What am I forgetting here?

- John



function ie_fix_events(t)
{
if (document.getElementById('body').currentStyle.scrollbarBaseColor)
{
if (t)
{
var e = document.getElementById(t).all;
for (var i = 0; i < e.length; i++)
{
if (e[i].getAttribute('onmouseover'))
{
var a = e[i].getAttribute('onmouseover');
e[i].attachEvent('onmouseover', function() {eval(a);});
}
}
}
}
}

JAB Creations
02-07-2010, 02:08 AM
Apparently there is a jQuery answer to this problem however I'm not using jQuery nor am I going to implement it just to fix this problem. Hopefully someone with a bit more insight in to this will have a better idea of what I'm talking about however...

http://jquery-howto.blogspot.com/2009/03/only-last-element-is-boundinsertedetc.html

JAB Creations
02-07-2010, 03:15 AM
So I resolved the issue by eventually finding one of my own blog posts on how I resolved it.

The answer is to create a second function which you call. I use alert to make sure I'm applying the correct code. Why I can't apply the event listeners inside of the first function I still don't really understand but creating a separate function is how I was able to conquer IE's face.