8000 GitHub - RudolfHattenkofer/instaction_js: Runs actions instantly on newly inserted DOM elements with a given selector.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

RudolfHattenkofer/instaction_js

Repository files navigation

instaction.js

Lets you register selectors for elements with a callback that is run every time a matching DOM element is added to the page. This can be useful if you have a lot of elements that are loaded via AJAX or otherwise added later on.

The most obvious use-case is replacing select tags on dynamically loaded forms with something more fancy, e.g. Chosen

Live demo: rudolfhattenkofer.github.io/instaction_js/

Installation

Rails

Install the gem:

gem 'instaction_js', git: 'git://github.com/RudolfHattenkofer/instaction_js'

Require in your Javascript source file:

//= require instaction

Define actions!

Instaction.register('.some-klass', function( $node ) {
    // Do stuff
});

Other

This is packed as a Rails gem for ease of use, but for non-Rails projects you can just grab this file: app/assets/javascripts/instaction.js

<script src="instaction.js"></script>

Usage

There is currently only one option this library has: How to process a selector. Either you pass a class name, the default:

Instaction.register('.some-klass', function( $node ) {
    // Do stuff
});

or a jQuery selector:

Instaction.register('[data-tooltip]', 'jquery', function( $node ) {
    // Do stuff
});

That’s it!

Important notes

Make to sure to require this script in the header, since only elements added to the DOM are processed. If it is loaded at the bottom of the page, all the elements will already be existing and the callback fires only for future insertions!

About

Runs actions instantly on newly inserted DOM elements with a given selector.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  
0