Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Javascript events - crossbrowser

Here's a two useful functions to manipulate events in javascript. First one allows you to add new event. And second one will return you element, which fired event.

//add event
var add_event = function(element, type, listener){
    if(element.addEventListener)
    {
        element.addEventListener(type, listener, false);
    }
    else
    {
        element.attachEvent('on' +  type, listener);
    }
};

//get element that fired event
var get_event_target = function(event){
    if(!event)
    {
        return window.event.srcElement;
    }
    else if(event.target)
    {
        return event.target; 
    }
    else
    {
        return event.srcElement;
    }
}

//example usage
add_event(document.getElementById("someid"), "click", 
    function(e){
        var elem = get_event_target(e);
        alert("element with id: " + elem.id + " was clicked");
    }
);

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus