UNPKG

4.07 kBMarkdownView Raw
1# dom-create-element-query-selector
2
3![Node](https://img.shields.io/node/v/dom-create-element-query-selector.svg?style=flat-square)
4[![NPM](https://img.shields.io/npm/v/dom-create-element-query-selector.svg?style=flat-square)](https://www.npmjs.com/package/dom-create-element-query-selector)
5[![Travis](https://img.shields.io/travis/hekigan/dom-create-element-query-selector/master.svg?style=flat-square)](https://travis-ci.org/hekigan/dom-create-element-query-selector)
6[![Coverage Status](https://coveralls.io/repos/github/hekigan/dom-create-element-query-selector/badge.svg?branch=master)](https://coveralls.io/github/hekigan/dom-create-element-query-selector?branch=master)
7
8> A utility function to create DOM elements with CSS selector-like syntax
9
10### Description
11
12As I had to use vanilla Javascript for a project, I got upset with how verbose it was to create DOM elements.
13
14I use the same css-selector-like syntax as `document.querySelector()` to create the new Nodes in a more compact, simple and readable way.
15
16There are no dependencies and multiple versions are available (es5, es6, UMD). See the BUILD section below for more information.
17
18### Usage
19
20#### Basic
21
22The simplest example add an empty `div` tag to the document's `body`.
23```js
24import createElement from 'dom-create-element-query-selector';
25
26const body = document.querySelector('body');
27body.appendChild(createElement());
28```
29
30#### Other usages
31```js
32import createElement from 'dom-create-element-query-selector';
33
34let elt = null;
35
36// some examples
37
38elt = createElement(); // <div></div>
39
40// create a span node with an id
41elt = createElement('span#my-id'); // <span id="my-id"></span>
42
43// add class
44elt = createElement('span.my-class'); // <span class="my-class"></span>
45
46// add id and class
47elt = createElement('span#my-id.my-class'); // <span id="my-id" class="my-class"></span>
48
49// add class and attributes
50elt = createElement('a[href=#].link'); // <a class="link" href="#"></a>
51
52// add content to the new element (text & other nodes)
53elt = createElement('div',
54 'paragraphs',
55 createElement('p', 'paragraph 1'),
56 createElement('p', 'paragraph 2')
57);
58// <div>
59// paragraphs
60// <p>paragraph 1</p>
61// <p>paragraph 2</p>
62// </div>
63
64// add the generated element to the DOM
65document.querySelector('body').appendChild(elt);
66
67```
68
69### Installation
70
71Install via [yarn](https://github.com/yarnpkg/yarn)
72
73 yarn add dom-create-element-query-selector (--dev)
74
75or npm
76
77 npm install dom-create-element-query-selector (--save-dev)
78
79
80### Examples
81
82See [`example`](example/script.js) folder or the [runkit](https://runkit.com/hekigan/dom-create-element-query-selector) example.
83
84### Builds
85
86If you don't use a package manager, you can [access `dom-create-element-query-selector` via unpkg (CDN)](https://unpkg.com/dom-create-element-query-selector/), download the source, or point your package manager to the url.
87
88`dom-create-element-query-selector` is compiled as a collection of [CommonJS](http://webpack.github.io/docs/commonjs.html) modules & [ES2015 modules](http://www.2ality.com/2014/0
89 -9/es6-modules-final.html) for bundlers that support the `jsnext:main` or `module` field in package.json (Rollup, Webpack 2)
90
91The `dom-create-element-query-selector` package includes precompiled production and development [UMD](https://github.com/umdjs/umd) builds in the [`dist` folder](https://unpkg.com/dom-create-element-query-selector/dist/). They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. You can drop a UMD build as a [`<script>` tag](https://unpkg.com/dom-create-element-query-selector) on your page. The UMD builds make `dom-create-element-query-selector` available as a `window.createElement` global variable.
92
93### License
94
95The code is available under the [MIT](LICENSE) license.
96
97### Contributing
98
99We are open to contributions, see [CONTRIBUTING.md](CONTRIBUTING.md) for more info.
100
101### Misc
102
103This module was created using [generator-module-boilerplate](https://github.com/duivvv/generator-module-boilerplate).