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

dom-ops

v1.5.4

Published

Manipulate the DOM of an element

Downloads

3

Readme

dom-ops

Install

npm install --save dom-ops

Basic Usage

<!DOCTYPE html>
<html>
    <head>
    <title>dom ops!</title>
    </head>
<body>
    <div id="location"></div>
    <script>
    var domOps = require('../index.js'),
        div = domOps(document.querySelector('#location'), function(content, data){
            return '<p>'+content+'</p>';
        });

    //Append a bunch of things inside paragraphs to #location
    for(var i=0; i<10; i++){
        div.append('thing '+i);
    }
    </script>
  </body>
</html>

domOps(element, template callback)

Construct a new dom-ops object for a DOM element.

The template callback is optional. If you supply a template callback every dom operation method from a domOps instance will use that callback as a filter before the dom operation.

In the following documentation everywhere you see the word toAdd this is a value that can be any of these:

  • DOM element
  • HTML string
  • Array of elements
  • An array like object (DOM library instance)

The return value from the template callback should be the same types as toAdd.

Any time you see the word data this means a value of any kind that is passed to the template function passed to the domOps constructor.

The word element refers to a DOM element passed to the first argument of domOps constructor.

Methods

appendTo(parent)

Append element to parent.

append(toAdd, data)

Append a value to element.

prepend(toAdd, data)

Prepend a value to element.

insert(toAdd, index|child, data)

Insert a value at index, or before the specified child.

replace(toAdd, index|child, data)

Replace a child at index, or the matched child with toAdd.

remove(index|child) child

Remove the child at index, or the matched child. Returns the removed child.

If you pass an index you'll get the template returned. If you pass a child node remove will return that.

indexOf(child)

Get the index of the child of element.

indexOf will still work if you have a template set. If the child is not a direct descendant of element a search through parent nodes is done before the index is looked for.

Any children of children of children, ... and so on passed to indexOf will get the index of the the top most parent that is a child of element.

indexOf is used internally so DOM operations like replace will work to replace any value added by itself, or contained in a template you provide. Alternatively you can just pass an index to a method that accepts an index.

Keep reference variables to the children of a template if you need to operate on those later.

If you use operations like append with strings the same strings can be used with indexOf. The look up for a string in indexOf is not discriminatory in any way so know that only the first match will be used. The algorithm is very simple so there will sometimes be false positives if your input string is not very unique.

match(RegExp|attribute name, RegExp) Array

Match the element, or children based on a regular expression.

The second argument is optional.

A regular expression in the first argument matches textContent of elements. Matched elements are returned in an array.

If you pass a string to the first argument match will look for attribute names that are the same as that string.

The regular expression in the second argument will match on attribute values, and return those elements that have those attributes matched.

//All elements with textContent = "contents of element" return.
var listOfElements = div.match(/contents of element/);
//All elements with the attribute data-store = "something" are returned.
var elmentsWithAttribute = div.match('data-store', /something/);

get(index)

Get the child, or child template at the child index of element.

html(string)

Pass a string to set innerHTML. Pass nothing to get innerHTML returned.

The html method is mostly for completeness. It could mostly be useful for clearing the contents of element.

The template callback is not called with the html method.

Properties

root

The element set with the constructor.

children

A reference to the element's children. These will be templates if you provide one.

classList

A reference to the element's classList object property. For using DOM classList operations.

recent

The last added child of element.

style

A reference to the element's style property.

Static Methods

domFrom(value)

Get a DOM element, or fragment from a value.

value can be any of these:

  • DOM element
  • HTML string
  • Array of elements
  • An array like object (DOM library instance)

See dom-from for more details.

linkCSS

Add a CSS link to the head node.

See link-css.

More Info

This module is only for DOM operations. Hopefully it will stay that way. :)