• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery plugin for dynamically created elements initialization

Note from author (pie6k). I've created this lib few years ago and it was nice back then. Now you should probably not be using jQuery for things like that and go with React or something similar. Thank you. —2018-11-21

Bug reports are still accepted.

jQuery.initialize

jQuery.initialize plugin is created to help maintain dynamically created elements on the page.

Synopsis

jQuery.initialize will iterate over each element that matches the selector and apply the callback function. It will then listen for any changes to the Document Object Model and apply the callback function to any new elements inserted into to the document that match the original selector.

$.initialize([selector], [callback]);

This allows developers to define an initialisation callback that is applied whenever a new element matching the selector is inserted into the DOM. It works for elements loaded via AJAX also.

Simple demo - click here

Example of use

$.initialize(".some-element", function() {
    $(this).css("color", "blue");
});

But now if new element matching .some-element selector will appear on page, it will be instantly initialised. The way new item is added is not important, you do not need to care about any callbacks etc.

$("<div/>").addClass("some-element").appendTo("body"); //new element will have blue color!

Unobserving

To cease observation of the document, you may disconnect the observer by calling disconnect() on the returned MutationObserver instance which stops it from receiving further notifications until and unless observe() is called again. . E.g.,

var obs = $.initialize([selector], [callback]); // Returns MutationObserver
obs.disconnect();

Options

target

By default, the entire document is observed for changes. This may result in poor performance. A specific node in the DOM can be observed by specifying a target:

$.initialize(".some-element", function() {
    $(this).css("color", "blue");
}, { target: document.getElementById('observe-this-element') });

Otherwise, target will default to document.documentElement.

observer

A custom MutationObserverInit may be provided. If not provided, it will default to internal configuration.

Browser Compatibility

Plugin is based on MutationObserver. It will works on IE9+ (read note below) and every modern browser.

Note: To make it work on IE9 and IE10 you'll need to add MutationObserver polyfill - like ones here: https://github.com/webcomponents/webcomponentsjs


Performance test (thanks to @bezborodow and @liuhongbo)

Todo

  • make it bower and npm compatible, add advanced performance test.

Contributors

More Repositories

1

react-ios-corners

iOS like border-radius corners react component
TypeScript
138
star
2

react-native-dev-menu-on-touch

Open dev menu with 3 fingers touch in react native instead of shake gesture
TypeScript
74
star
3

hooksy

Simple app state management based on react hooks
TypeScript
62
star
4

motionblur

Experiment with motion blur website scrolling
TypeScript
60
star
5

demotime

TypeScript
39
star
6

filterer

TypeScript
24
star
7

use-method

Hook for creating callbacks that behave like class methods - function keeps the same reference during entire component lifecycle and always have access to 'fresh' variables from last render.
TypeScript
19
star
8

theme-composer

TypeScript
19
star
9

parsebuddy

Advanced parser of arbitral input string
TypeScript
18
star
10

react-smart-sticky

SmartSticky is React component for sticky topbar that will hide when scrolling down and show when scrolling up.
JavaScript
14
star
11

fuzzystring

🕵 Simple fuzzy search library written in TypeScript
TypeScript
12
star
12

statek

State management library based on proxies.
TypeScript
9
star
13

react-native-animated-manager

Simple class wrapping react native Animated Value that helps to perform various operations on them
TypeScript
7
star
14

easy-https

Zero config, one step local https server with a custom local domain.
TypeScript
7
star
15

suspensify

Easy way to convert any async function to suspended function
TypeScript
7
star
16

serverless-typescript-starter

Example of serverless lambda with typescript and serverless-offline
JavaScript
7
star
17

react-native-toolbox

This is set of common react-native tools.
TypeScript
3
star
18

use-needed-state

It's like `useState` - but it'll spy what part of the state is actually used during the render and re-render only if needed.
TypeScript
2
star
19

cleanups

Simple, yet powerful utility for managing cleanups in JavaScript.
TypeScript
1
star