Nodejs HTML Element Event Add delegate(eventName, elementSelector, cb)

Here you can find the source of delegate(eventName, elementSelector, cb)

Method Source Code

Element.prototype.delegate = function(eventName, elementSelector, cb) {
  var _this = this

  _this.addEventListener(eventName, function(evt) {
    var _this = evt.target

    if (_this.is(elementSelector)) {
      cb.call(_this, evt)/*w  w  w  .  ja v  a  2 s  .c o  m*/
    }
    if (_this.parentNode.is(elementSelector)) {
      cb.call(_this.parentNode, evt)
    }
  })
}

Related

  1. addEvent(event, callback, bubble)
    HTMLElement.prototype.addEvent = function (event, callback, bubble) {
        this.addEventListener(
            event,
            function (event) {
                try {
                    callback.apply(this, arguments);
                } catch (e) {
                    console.log(e);
                event.preventDefault();
                return false;
            },
            bubble);
    };
    
  2. addClickEvent(func)
    HTMLElement.prototype.addClickEvent = function addClickEvent(func) {
        this.addEvent("click", func, false);
    };