...

View Full Version : EventListeners: Nearly identical code; differing results, a mystery.



danielbdavis
12-11-2010, 06:50 AM
In building a web page, I needed to learn about event listeners. I built a small testPage, consisting of 3 tables: an outer one containing 2 inner tables which appeared side-by-side. The effect targeted was rollover (or mouseover, as it is now called). I chose the simplest effect I could think of - background change, coded it, and it worked! Accordingly, I went on to my real desired effect: visibility. (Not display or non-display, but visible or not-visible.)

Accordingly, I duplicated the working code, but it did not work. The browser is Firefox 5.0, and the error pane shows nothing. It is mystifying. The only difference is that the visibility code that works has been used outside of an event listener. The relevant JavaScript code appears below.

The outer table is "outerTable" the inner table with the working color change is "rollTable", and the inner table with the non-working visibility is "optionsTable". Note that the visibility-switching statements have been independently tested elsewhere and they work just fine, as exemplified by the inititializing call to setvis on the 'optionsTable' to hide it at the start! Note further that the alerts for 'doshow' and 'dohide' never fire, nor is there an error indication.

<script type="text/javascript">

var dored;
var doblue;

var doshow;
var dohide;

function registerEventListeners() {
var rt = document.getElementById("rollTable");
var ot = document.getElementById("optionsTable");

alert("register");
rt.addEventListener("mouseover",setred,false);
rt.addEventListener("mouseout",setblue,false);
dored = function() { rt.bgColor = "red" };
doblue = function() { rt.bgColor = "blue" };

setvis("optionsTable",-1);

alert("optionsTable");

doshow = function() {
alert("doshow");
ot.style.visibility="visible";
}

dohide = function() {
alert("dohide");
ot.style.visibility="hidden";
}

ot.addEventListener("mouseover",setshow,false);
ot.addEventListener("mouseout",sethide,false);

}

function setred() { dored(); }
function setblue() { doblue(); }

function setshow() { alert("setshow"); doshow(); }
function sethide() { alert("sethide"); dohide(); }

</script>

Many thanks for any and all help.

Kor
12-11-2010, 10:33 AM
If your function registerEventListeners() is never triggered, the global variables above are never defined.

But your whole code is rather a mismatch. addEventListener() will not work under IE (at list not under IE<=8)

See: http://www.quirksmode.org/js/events_advanced.html

I don't understand why do you create such an intricate code (with functions declared as global variables, but defined inside another function). Why don't you pass the arguments to a a function? Or use a constructor. Or use the prototype method


But, if you simply want to create a new event and to attach it to an element, you may simply use DOM0 syntax and a closure:



var element=document.getElementById('elementID');
element.onmouseover=function(){this.style.backgroundColor='red';otherFunction()}
element.onmouseout=function(){this.style.backgroundColor='blue';someOtherFunction()}

or without the closure


var element=document.getElementById('elementID');
element.onmouseover=setRed;
element.onmouseout=setBlue;

function setRed(){
this.style.backgroundColor='red';
otherFunction()
}

function setBlue(){
this.style.backgroundColor='blue';
someOtherFunction()
}



Try always to keep the code clear and simple.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum