Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript equivalent to jquery trigger method

Tags:

javascript

If this is the equivalent to the jquery bind method, what would it be for the trigger method.

function bind( scope, fn ) {
    return function () {
        fn.apply( scope, arguments );
    };
}

the code above is from another post and it looks the same as a proxy method

you can comment on this too

I have to take the jquery part out off this framework, - this is just the relevant part

if (selector === '') {
              this.el.bind(eventName, method);
            } else {
              this.el.delegate(selector, eventName, method);
            }
          }
        }
      });

      if (includes) result.include(includes);

      return result;
    };

    exports.Controller = mod;
})($, window);

var exports = this;

var Events = {
    bind: function(){
      if ( !this.o ) this.o = $({});
      this.o.bind.apply(this.o, arguments);
    },
trigger: function(){
  if ( !this.o ) this.o = $({});
  this.o.trigger.apply(this.o, arguments);
}
};

thanks

like image 808
Richard Avatar asked Mar 09 '26 17:03

Richard


2 Answers

It depends on the type of event you wish to trigger. If it's a custom event:

var event = new Event('build');
elem.dispatchEvent(event);

If it's a native event:

var event = new MouseEvent('click');
elem.dispatchEvent(event);

This is of course meant to simulate a mouse event. Other events have their own type.

like image 173
Naor Biton Avatar answered Mar 12 '26 07:03

Naor Biton


Once I crossed this site How to Manually Trigger Events in JavaScript

// Here is a VERY basic generic trigger method
function triggerEvent(el, type)
{
    if ((el[type] || false) && typeof el[type] == 'function')
    {
        el[type](el);
    }
}

// We could call this on multiple objects at any time
function resetFields()
{
    triggerEvent(document.getElementById('has-email'), 'onchange');
    triggerEvent(document.getElementById('other-field'), 'onclick');
    triggerEvent(document.getEleemntById('another-one'), 'onblur');
}
like image 28
Praveen Avatar answered Mar 12 '26 07:03

Praveen