Giter Club home page Giter Club logo

added2dom's Introduction

added2dom

added2dom is a jquery plugin that executes a callback when the matched elements are added to the DOM.

Usage:

CoffeeScript:

$div = $('<div/>')
$div.added2dom ->
  # do something dom specific
  $div.text $div.height()

# the callback will be triggered
# when the $div is added to the dom
$("body").append($div)
# It also works with multiple elements
$div1 = $('<div>One</div>')
$div2 = $('<div>Two</div>')
$div1.add($div2).added2dom ->
  console.log $(this).text()

$("body").append($div1) # output: One
$("body").append($div2) # output: Two

JavaScript:

var $div = $('<div/>');

$div.added2dom(function() {
  // do something dom specific
  return $div.text($div.height());
});

// the callback will be triggered
// when the $div is added to the dom
$("body").append($div);
// It also works with multiple elements
var $div1 = $('<div>One</div>');
var $div2 = $('<div>Two</div>');
$div1.add($div2).added2dom(function() {
  return console.log($(this).text());
});

$("body").append($div1);
$("body").append($div2);

Requirements

Installation

  • JavaScript: Use /lib
  • CoffeeScript: Use /src

How it works

This uses the new MutationObserver object to listen for mutations to a specified root node and checks each mutation to see if it was one of the elements we care about being added. If MutationObserver is not supported then it falls back to a polling system that checks the root node to see if it contains any matched elements every iteration.

You can set the root node and the interval when calling added2dom:
CoffeeScript:

$el.added2dom (->
  # run when added to dom
),
  root: $("#main") # defaults to document
  poll: 5000       # defaults to 100

JavaScript:

$el.added2dom((function() {
  // run when added to dom
}), {
  root: $("#main"), // defaults to document
  poll: 5000        // defaults to 100
});

Caveat

This stops listening once the element is added, so if you plan to add the same element to the dom multiple times you will need to add this each time. I may add support for this if I get requests but I don't currently feel the need to.

endorse

added2dom's People

Contributors

jisaacks avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.