Giter Site home page Giter Site logo

kbismark / eventjs Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 36 KB

Use the onEvent methods of HTML elements just like using addEventListener. Attach unlimited event listeners to elements.

License: MIT License

JavaScript 100.00%
events javascript browser-events event-emitter dom

eventjs's Introduction

Event.js

Event.js is a javascript event tool that can be used in your front-end applications. It uses the traditonal onEvent methods of HTML elements but gives you the same benefits you get from using the modern addEventListener method and even more features.

  • Removes the main downside of using the traditional DOM events, i.e you can add as many event listeners as you want just like using the addEventListener method

  • Easy manipulations on targeted elements.

  • Chainable.

  • Event.js has all the features of the addEventListener method.

  • Event.js is supported in many browsers (modern browsers + old browsers).

Methods

  • targ or $ EventListenerObject.targ(elements) | EventListenerObject.$(elements)

Use to set object(s) for event manipulations.

var divs = document.getElementsByTagName("div");
var events = new EventListener();// Returns EventListenerObject.

events.targ(divs) 
//or do this 
events.$(divs)
  • on .on(eventName, handler, optional-identifier)

Use to add event listeners.

 events.targ(divs).on("mouseover",function(eventObject,targetElements){

   // "this" refers to the element among the targeted elements that is focused or hovered.
   this.style.color="red";
   console.log(this===eventObject.target);// true
   
   // Also take advantage of the `each` method of the targetElements parameter
   // It iterates over all the targeted elements and performs the function passed as argument to it.
   targetElements.each(function(){
       // Note: "this" here is not the same as the "this" above.
       console.log(this);
   });
   
},"my optional identifier for the anonymous function")/* chainable */

.on("mouseout",function(eventObject, targetElements){

   // "this" refers to the element among the targeted elements that is blured.
   this.style.color="blue";
   console.log(this===eventObject.target);// true
})
/* Add another mouseover listener */
 .on("mouseover",function(eventObject, targetElements){

   console.log(eventObject);// Mouse Event Data
});//Chaining ends here but event.js is not ended yet.

//  Add another mouseover listener
events.targ(divs).on("mouseover",function(eventObject, targetElements){

   console.log(eventObject);// Mouse Event Data
   console.log(targetElements);// An array of the Targeted Elements with the each method attached to the array.
});
  • removeAllListeners .removeAllListeners(eventName)

Use to remove all event listeners/handlers of the event name specified.

// Removes all mouseover listeners/handlers of the first and second Div elements.
events.targ([divs[0],divs[1]]).removeAllListeners("mouseover");
  • eventCount .eventCount(eventName, count)

Use to set how many times an event listener/handler will be called.

// Sets the mouseover listeners to be called once.
events.targ(divs).eventCount("mouseover",1);// Results:
                                                 // Listeners are executed when a targeted element is hovered for the first time 
                                                 // and do not get executed afterwards when hovered again.
  • removeListener .removeListener(eventName, the-optional-identifier-for-the-listener | A reference to the listerner)

Use to remove an event listener/handler of the specified event name.

 var button = document.getElementById("button");
 
 // Removing listener by identifier
 events.targ(button).on("click",function(e,ech){
     //...
     
 },"buttonClickedListener1").on("click",function(e,ech){
     //...
     
 },"buttonClickedListener2").removeListener("click","buttonClickedListener1");
 // If the button is clicked, only the second listener gets executed.
 
 // Removing listener by reference
 function buttonIsClicked(eventObject,targetElements){
     //do stuff..
 };
 
 events.targ(button).on("click",buttonIsClicked).on("click",function(e,ech){
     //...
 },"buttonClickedListener2").removeListener("click",buttonIsClicked);
 // If the button is clicked, the buttonIsClicked listner does not get executed.
 
  • removeTargetEvents .removeTargetEvents(targetElements | undefined)

Use to remove all listening events of the object(s) specified.

/* Let's go back to the divs examples above */

// Removes all listening events of the targeted elements
events.targ(divs).removeTargetEvents();

 // Removes all listening events of the element(s) passed as argument to the removeTargetEvents method.
 events.targ(divs).removeTargetEvents(button);
 
`

Note: If you choose to use event.js, do not use the onEvent properties of the HTML elements. Use event.js throughout for all that you will use the onEvent properties of the HTML elements

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.