Giter Club home page Giter Club logo

dynamic-listener's Introduction

Dynamic event listener

A quick pure JavaScript implementation that allows to add an event listener that also works on elements dynamically created. The functionality is similar to jQuery.on().

The minified library has only 806 Bytes.

Limitations

You should use focusout event instead of blur, as the blur event does not bubble.

Usage

Include the library and call addDynamicEventListener to create a new event listener.

<script src="dynamicListener.min.js"></script>
<script>
// Any `li` or element with class `.myClass` will trigger the callback, 
// even elements created dynamically after the event listener was created.
addDynamicEventListener(document.body, 'click', '.myClass, li', function (e) {
    console.log('Clicked', e.target.innerText);
});
</script>

Note: You will have to use event.delegatedTarget in your callbacks to get the element that matches the selector (as event.target could be a child of this element and event.currentTarget is the root on which the listener was attached to).

API

addDynamicEventListener(rootElement, eventType, selector, callback, options)
Parameters
Name Type Description
rootElement Element The root element to add the listener to.
eventType string The event type to listen for.
selector string The selector that should match the dynamic elements.
callback function The function to call when an event occurs on the given selector.
options boolean object Passed as the regular options parameter to the addEventListener function Set to true to use capture.
Usually used as an object to add the listener as passive
Returns
  • Void

Browser support

Same as addEventListener (consider it to be IE9+).

How it works

The script uses the Element.matches() method to test the target element against the given selector. When an event is triggered the callback is only called if the target element matches the selector given.

License

No restrictions. Feel free to use it in any project you want.

Check out my analytics script

userTrack
Self-hosted analytics plaform with heatmaps and full session recordings.

dynamic-listener's People

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.