Skip to content

A toy JavaScript library for quickly creating DOM elements

License

Notifications You must be signed in to change notification settings

reedperry/minidom

Repository files navigation

minidom

minidom is a small JavaScript library for creating and adding DOM elements to a page.

Creating Elements

There are two ways to create elements using minidom: elem and build.

import { build, elem } from 'minidom';

elem is the simplest API, useful when you can describe everything you need using a string similar to a CSS selector.

elem('p#intro');                   // <p id="intro"></p>

elem('div.banner');                // <div class="banner"></div>

elem('img[src="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/uploads/a.jpg"]'); // <img src="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/uploads/a.jpg">

build is more involved, but provides more flexibility.

const square = {
  width: '100px',
  height: '100px',
  display: 'inline-block'
};

const rounded = {
  borderRadius: '10%'
};

const box = md
  .build('div')
  .withStyle(square)
  .withStyle(rounded) // styles can be composed
  .on('mouseenter', function () {
    this.style.opacity = 0.5
  })
  .on('mouseleave', function () {
    this.style.opacity = 1
  })
  .render(); // this returns the final element with the builder properties applied

About

A toy JavaScript library for quickly creating DOM elements

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published