[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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>
0
prob
Asked:
prob
1 Solution
 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now