Adding events to objects using the DOM with addEventListener

I have a wiki editor I am writing. I want to make it as DOM and browser friendly as possible. I am trying to use the DOM addEventListener action to add mouseover events to a button.

I have found a 'wrapper' for DOM 2 and IE but it is not working.

Here is a code excerpt.

A second problem is getting the AccessKey feature to work...



<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

//
//      Is the browser DOM compatible?
//

var W3CDOM = (document.createElement && document.getElementsByTagName);

//
//      Adding events the browser compatible way - see
//      http://www.sitepoint.com/print/behaved-dhtml-case-study
//


function addEvent(objObject, strEventName, fnHandler) {
 // DOM-compliant way to add an event listener
 if (objObject.addEventListener)
   objObject.addEventListener(strEventName, fnHandler, false);
 // IE/windows way to add an event listener
 else if (objObject.attachEvent)
   objObject.attachEvent("on" + strEventName, fnHandler);
}


//
//       loop through buttons and add event handlers
//

window.onload=addimgfunctions
function addimgfunctions()
{
      if (!W3CDOM)
      {
            return;
      }
      var imgs,i;
      imgs=document.getElementsByTagName('img');
      for(i in imgs)
      {
            if(/button/.test(imgs[i].className))
            {
            
addEvent(imgs[ i], "mouseover", mouseover(this));            
//imgs[ i].onmouseover = function(){mouseover(this);};
imgs[ i].onmouseout = function(){mouseout(this);};
imgs[ i].onmousedown = function(){mousedown(this);};
imgs[ i].onmouseup = function(){mouseup(this);};
}
}
}

// Mouse over functions to animate the buttons

function mouseover(el) {
el.className = "raised";
}

function mouseout(el) {
  el.className = "button";
}

function mousedown(el) {
  el.className = "pressed";
}

function mouseup(el) {
  el.className = "raised";
}

</script>
<style>
#toolbar      {      
                  margin: 0;
                  padding: 0;
                  width:578px;
                  background-color: buttonface;
                  border-top: 1px solid buttonhighlight;
                  border-left: 1px solid buttonhighlight;
                  border-bottom: 1px solid buttonshadow;
                  border-right: 1px solid buttonshadow;
                  text-align:left;
                  vertical-align: middle;
                  cursor: hand;

                    }
.button       {
                  background: buttonface;
                  border: 1px solid buttonface;
                  margin: 1px;
                  text-align: center;
                    vertical-align:middle;
                  cursor: hand;
                  }


.raised            {
                  border-top: 1px solid buttonhighlight;
                  border-left: 1px solid buttonhighlight;
                  border-bottom: 1px solid buttonshadow;
                  border-right: 1px solid buttonshadow;
                  background: buttonface;
                  margin: 1px;
                  }
                  
.pressed      {
                  border-top: 1px solid buttonshadow;
                  border-left: 1px solid buttonshadow;
                  border-bottom: 1px solid buttonhighlight;
                  border-right: 1px solid buttonhighlight;
                  background: buttonface;
                  margin: 1px;
                  }

.img            {
                  cursor: normal;
                  height: 22px;
                  width: 23px;
                  cursor: hand;
                  vertical-align: middle;
                  }

</style>
   </head>
   <body>
<div  id="toolbar">
 <img class="button wikimarkup" align="middle" value="" src="UI_bold.gif" ACCESSKEY="B" tabindex="1" alt="Make selection bold" onclick="alert('you clicked me')">
</img>
</img>
</div>
   </body>
</html>
probAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
This line:
addEvent(imgs[ i], "mouseover", mouseover(this));

Should be like this:
addEvent(imgs[ i], "mouseover", function(){mouseover(this)});

If your funstion would not take parameters, then you could write it like this:
addEvent(imgs[ i], "mouseover", mouseover);

But then the mouseover function has to be like this:
function mouseover(e) {
  if(!e) e=event;
  var el = (e.srcElement)?e.srcElement:e.target;
  el.className = "raised";
}

Then you can assign the upper function like I stated before:
addEvent(imgs[ i], "mouseover", mouseover);

The parameter is passed by Netscape even when only function reference is assigned to event handler.



0
probAuthor Commented:
I am running this under IE6 and neither option works.

Any other thoughts?
0
probAuthor Commented:
Solved this by using another function to get the object that calls the event (from Danny Goodman)

function functionName(evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "")
  if (evt) {
    var elem
    if (evt.target) {
      elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
    } else {
      elem = evt.srcElement
    }
    if (elem) {
      // process event here
    }
  }
}

and used the reference to elem

ie  elem.className = "raised";

Now, for the 500 points, can you think why I can't use the accesskey?

I think its because the event has been added to the image and not the link. While I stumble around, here is an article about the problem...

http://www.onlinetools.org/articles/unobtrusivejavascript/chapter2.html

0
hallvorsCommented:
The accesskey works in Opera :)
It does not work in FireFox because alt-B is mapped to the Bookmarks menu.
IE probably requires a link.
0
moduloCommented:
PAQed with no points refunded (of 500)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.