Forum Moderators: open

Message Too Old, No Replies

IE: Loop applies last value to all elements?

a should = a, b should = b, but everything = z?

         

JAB Creations

9:57 pm on Feb 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

P.S. - The new WebmasterWorld BBcode validator forced me to change var i to var ii.

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

daveVk

12:52 am on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming e[ii].getAttribute('onmouseover') returns a function ? Try

e[ii].attachEvent('onmouseover', a );

JAB Creations

1:48 am on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not having trouble attaching events. Let me visualize what the problem is.

Let's say there are five elements. This is what I want...

a = a
b = b
c = c
d = d
e = e


However this is what is actually happening...
a = e
b = e
c = e
d = e
e = e


- John

JAB Creations

3:15 am on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

- John

daveVk

1:53 am on Feb 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your original code would execute

function() {eval(a);}

on each event, where a contains whatever it was last set to.

Your new code somehow encodes the value of a into the function so that this executes

function() {eval(...original value from a...);}

JAB Creations

2:46 am on Feb 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was trying to unset a though wasn't been able to figure out how to do that.

Sending the data to an independent function worked...and remember it's already in a scenario where I know only IE will ever use this function to begin with...so unless we can figure out definitively what the issue was I consider it case closed since it now works as desired.

Thanks for your input. :)

- John