OK, so I have a strange JS function problem for IE versions 8 and below. (who'd have guessed)
there's this function defined in the 'script' section of my page, it's supposed to change the background color of a node element on click:
function highlight(){
if (this.style.backgroundColor != "rgb(163, 167, 334)"){
this.style.backgroundColor = "rgb(163, 167, 334)";
}
}
basically, it's saying 'if it's not the given backgroundColor, make it that backgroundColor.'
In order to evoke the method, the same 'script' tag also contains:
button.attachEvent('onmousedown', highlight);
for some reason, I continually get the following error in the >IE9's developer tools console (running ie9 in compatibility mode as 'ie8'):
SCRIPT5007:unable to get value of the property 'backgroundColor':
object is null or undefined
I'm using 'attachEvent' in IE8 in the same way I'm using 'addEventListener' in Firefox, Chrome, and IE9. Unfortunately it seems to not behave in quite the same way.
I need this because my client insists on using IE8.
Any suggestions?
EDIT/SOLUTION FOUND: The problem was found. Highlight function references 'this'. When attachEvent fires it always understands 'this' as meaning 'the browser window', NOT the object that receives the action. To circumvent this problem, there are two ways:
element.onevent = function;
(in my case: button.onclick = highlight)
element[onevent] = function;
(in my case: button[onclick] = highlight)
Regarding the second variant, it's a bonus thing discovered (keeping this here for anyone stumbling). I'll just share it here:
a click event can actually be triggered by writing obj[onclick] = func. (in my case that was: "button[onclick] = highlight;" this is useful because it allows 'passing in' an event's name when necessary.
Thanks everyone for your help! Case closed.