Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Silver Spring, MD
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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:

    Code:
    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
    Code:
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •