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 |
|
12 | As I had to use vanilla Javascript for a project, I got upset with how verbose it was to create DOM elements.
|
13 |
|
14 | I use the same css-selector-like syntax as `document.querySelector()` to create the new Nodes in a more compact, simple and readable way.
|
15 |
|
16 | There 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 |
|
22 | The simplest example add an empty `div` tag to the document's `body`.
|
23 | ```js
|
24 | import createElement from 'dom-create-element-query-selector';
|
25 |
|
26 | const body = document.querySelector('body');
|
27 | body.appendChild(createElement());
|
28 | ```
|
29 |
|
30 | #### Other usages
|
31 | ```js
|
32 | import createElement from 'dom-create-element-query-selector';
|
33 |
|
34 | let elt = null;
|
35 |
|
36 | // some examples
|
37 |
|
38 | elt = createElement(); // <div></div>
|
39 |
|
40 | // create a span node with an id
|
41 | elt = createElement('span#my-id'); // <span id="my-id"></span>
|
42 |
|
43 | // add class
|
44 | elt = createElement('span.my-class'); // <span class="my-class"></span>
|
45 |
|
46 | // add id and class
|
47 | elt = createElement('span#my-id.my-class'); // <span id="my-id" class="my-class"></span>
|
48 |
|
49 | // add class and attributes
|
50 | elt = createElement('a[href=#].link'); // <a class="link" href="#"></a>
|
51 |
|
52 | // add content to the new element (text & other nodes)
|
53 | elt = 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
|
65 | document.querySelector('body').appendChild(elt);
|
66 |
|
67 | ```
|
68 |
|
69 | ### Installation
|
70 |
|
71 | Install via [yarn](https://github.com/yarnpkg/yarn)
|
72 |
|
73 | yarn add dom-create-element-query-selector (--dev)
|
74 |
|
75 | or npm
|
76 |
|
77 | npm install dom-create-element-query-selector (--save-dev)
|
78 |
|
79 |
|
80 | ### Examples
|
81 |
|
82 | See [`example`](example/script.js) folder or the [runkit](https://runkit.com/hekigan/dom-create-element-query-selector) example.
|
83 |
|
84 | ### Builds
|
85 |
|
86 | If 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 |
|
91 | The `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 |
|
95 | The code is available under the [MIT](LICENSE) license.
|
96 |
|
97 | ### Contributing
|
98 |
|
99 | We are open to contributions, see [CONTRIBUTING.md](CONTRIBUTING.md) for more info.
|
100 |
|
101 | ### Misc
|
102 |
|
103 | This module was created using [generator-module-boilerplate](https://github.com/duivvv/generator-module-boilerplate).
|