Coder Social home page Coder Social logo

ekansss / magicevent Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 114 KB

Use javascript event like you're a magician

License: MIT License

JavaScript 100.00%
event-emitter listener javascript vanilla event listeners events vanilla-javascript vanilla-js vanillajs

magicevent's Introduction

MagicEvent

MagicEvent is one easy and fast event handler in vanillaJS, it natively handle dynamically created Element ! Be the magician !

How it work

It is easy, just call MagicEvent.js in your page then start the fun

<script type="text/javascript" src="pathtoassets/MagicEvent.js"></script>
<script type="text/javascript">
  //here start the fun
<script>

What can we do ?

Listen to an event

To register an event listener just use on method

  //place an event listener for click on a tag
  MagicEvent.on("click", "a", myLinkClickFunc);

Multiple event at once

It handle mutiple event binding in one go

  //place an event listener for click, hover and touch on a tag
  MagicEvent.on("click hover touch", "a", myLinkClickFunc);

CSS Selector

You can select your elements directly by CSS Selector

  //place an event listener for click on all element with class .clickme, event if they were created dynamically
  MagicEvent.on("click", ".clickme", myClickFunc);

Chainning

All method return MagicEvent object to chain all your call

  //you can chain all your call
  MagicEvent.on("click", "a", myLinkClickFunc)
    .on("click", ".clickme", myClickFunc);

Delegate

Delegate is automatic for all elements, so it's easy to make it !(not available for scroll event)

  //register event listener with delegate
  MagicEvent.on("click", "a", myLinkClickFunc)

currentElement

Due to delegate, event.currentTarget is always document, but you can use event.delegateTarget (except for scroll)

  function(event){
    //get <a> element that was click 
    const el = event.delegateTarget;
    //magic here
  }
  
  MagicEvent.on("click", "a", myLinkClickFunc)

Execute only once

If you want a listener to be executed only once, use one method

  //place an event listener that will execute only once per HTML element
  MagicEvent.one("click", "a", myOnceLinkClickFunc);

Remove listener

You can remove an event listener with off method

  //remove existing listener
  MagicEvent.on("click", "a", myLinkClickFunc)
    .off("click", "a", myLinkClickFunc);

Emit an event

Do you want to emit an event on precise element just use emit method

  // emit an event with bubbling on (event must be in listen by MagicEvent)
  // emit click on all 'a' tag
  MagicEvent.emit("click", a);
  // emit event with custom data
  MagicEvent.emit("click", a, {simulatedClick: true})

Custom events

Event Handler handle custom event like it's nothing, just use it on normal way

  // place event listenner on custom event
  MagicEvent.on("supercustom", "a", funcForSuperCustom);

magicevent's People

Contributors

ekansss avatar

Stargazers

 avatar

Watchers

 avatar

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.