JavaScript implements a method to add multiple event handlers to a specified object

  • 2020-05-30 19:24:35
  • OfStack

This article illustrates how the JavaScript implementation adds multiple event handlers to a specified object. Share with you for your reference. The details are as follows:

If you want to do more than one thing at a time when an object is clicked, use the following code

/* Start of the multihandle Object...*/
function MultiHandle(owner){
 var my_handlers = new Array();
 var my_owner = owner;
 this.append = function(handler){
  my_handlers[my_handlers.length] = handler;
 } = function(evt){
  var i;
  for(i = 0; i < my_handlers.length; i++){
   my_owner.tempspace = my_handlers[i];
/* End of the multihandle object*/
/* start of the object add event handler script */
/*This bit goes where you'd normally write...
... object.onmouseup = [event handler]...
... where [event handler] is an existing function ...
... that handles an event, or even an
... anonymous function.*/
if(typeof(MultiHandle) != "undefined"){
 var mup_handler = object.mh_onmouseup;
  mup_handler = new MultiHandle(object);
  object.mh_onmouseup = mup_handler;
  object.onmouseup = function(evt){;
 mup_handler.append([event handler]);
 object.onmouseup = [event handler];

I hope this article has been helpful to your javascript programming.

Related articles: