npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

greenback

v1.2.3

Published

A light DOM manipulation tool built with jQuery-like methods

Downloads

6

Readme

Greenback


Introduction

Greenback is a lightweight DOM manipulation tool with jQuery-like syntax. I wanted a DOM manipulation tool that was easy to use and syntactically brief, but without the bloat that comes along with jQuery.

Greenback is written in es6/es2015, but is transpiled via Babel for npm usage.


Installation

npm install greenback --save-dev

Usage

Lets say for example that I wanted to create a new section with the class of example.

let $ = require('greenback');

let $section = $('section').class('example');

Then later on in my project, I wanted to attach that element to another I created called $header...

$section.append($header);

Like jQuery, DOM nodes can be assigned to variables, also known as caching, then referenced later.

Invoking

All methods must be used after invoking Greenback, so $.METHOD_NAME won't do much of anything. In addition, there are a few features to note about using it.

Creating a new DOM node

Unlike jQuery, Greenback is not designed to blindly grab all tags of a certain type. So this syntax will actually create a new DOM node.

let $tag = $('TAG_NAME');    // $('div'); would create a new div tag

Capturing multiple DOM nodes

One of the points of this library was not to make sweeping changes to DOM elements. If you do want to grab more than one DOM element at once, assign a class to them and invoke Greenback to capture them all.

let $classElements = $('.EXAMPLE_CLASS_NAME');

Getting the first element of a set

But even then there are times when you may just want the first element using that classname. In that case, you can use the pseudo-tag, first.

let $firstElement = $('.EXAMPLE_CLASS_NAME:first');

Element with multiple classes

You may want to capture an element with two (or more) classes.

let $active = $('.EXAMPLE_CLASS_NAME.EXAMPLE_ACTIVE_NAME');

By ID

Even though ID's are generally frowned upon, Greenback supports capturing them as well.

let $myId = $('#EXAMPLE_ID');

Methods

All methods are chainable.

find(string)

Finds an element(s) of the given type and returns a new reference to it.

let $rows = $('.fake-table').find('.row');

text(string)

Inserts text into the node

let $hello = $('div').text('Hello World!');

html(string)

Inserts html into the node

let $goodbye = $('div').html('<strong>Goodbye!</strong>');

attr(object)

Assigns attributes to the node. Note that this does not check to see if the attribute(s) is/are valid.

let $link = $('img').attr({ src: 'image.gif, border: 0});

class(string)

Assign a class, or classes, to a node

let $h1 = $('h1').class('large and-in-charge');

removeClass(string)

Removes a given class from a node

$h1.removeClass('large');

on(string, function)

Attaches a given event to the node.

$link.on('click', e => {
  console.log('I was clicked:', e);
});

Be careful! Right now Greenback does not have an off method. Events attached this way may lead to memory problems if the node the event is attached to is removed from the DOM

append(DOM node or Greenback Object)

Appends a node or Greenback object to the invoked Greenback object.

$h1.append( $someStuffAtTheBottom );

changed in 1.2.0

prepend(DOM node or Greenback Object)

Prepends a node or Greenback object to the invoked Greenback object.

$h1.prepend( $someStuffAtTheTop );

changed in 1.2.0

appendTo(DOM node or Greenback Object)

Appends the invoked Greenback object to a node or Greenback object.

$someStuffAtTheBottom.appendTo( $h1 );

added in 1.2.0

prependTo(DOM node or Greenback Object)

Prepends the invoked Greenback object to a node or Greenback object.

$someStuffAtTheTop.prependTo( $h1 );

added in 1.2.0