A functional DOM traversal and manipulation library for modern browsers
Døm was born out of the need of a minimal set of functional-style DOM utilities that could replace core jQuery in modern browsers. It relies only on the latest native browser APIs, making the library light-weight and fast. Much inspiration was taken from You Don't Need jQuery.
$ yarn add doem
import {find, html, [...]} from 'doem';
const el = find(document, '.foo');
html(el, 'Hello World!');
Add a class to an element.
Parameters
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
addClass(element, 'foo');
<p class=foo>Lorem ipsum</p>
Insert HTML after an element.
Parameters
element
Element The element to insert the HTML after.html
string The HTML to insert after the element.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
after(element, '<p>Dolor sit amet</p>');
<p>Lorem ipsum</p><p>Dolor sit amet</p>
Insert HTML at the end of an element.
Parameters
element
Element The element to insert the HTML at the end of.html
string The HTML to insert at the end of the element.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
append(element, '<b>dolor sit amet</b>');
<p>Lorem ipsum<b>dolor sit amet</b></p>
Get or set the value of an attribute of an element.
Parameters
element
Element The element whose attribute to get or set.name
string The name of the attribute to get or set.value
string= The value of the attribute if setting.
Examples
<img title="Lorem ipsum">
const element = find(document, 'img');
attr(element, 'title');
// => 'Lorem ipsum'
attr(element, 'title', 'Dolor sit amet')
<img title="Dolor sit amet">
Returns string The value of the attribute if getting.
Insert HTML before an element.
Parameters
element
Element The element to insert the HTML before.html
string The HTML to insert before the element.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
before(element, '<p>Dolor sit amet</p>');
<p>Dolor sit amet</p><p>Lorem ipsum</p>
Get all the children of an element.
Parameters
element
Element The element whose children to get.
Examples
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
const element = find(document, 'ul');
children(element);
// => [<li>Item 1</li>, <li>Item 2</li>]
Returns Array.<Element> The children of the element.
Create a deep copy on an element.
Parameters
element
Element The element to copy.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
element !== clone(element);
// => true
Returns Element The copy of the element.
Get the closest matching descendant of an element.
Parameters
Examples
<ul class="lvl-1">
<li class="item-1">Item 1
<ul class="lvl-2">
<li class="item-2">Item 2</li>
</ul>
</li>
</ul>
const element = find(document, '.item-2');
closest(element, 'ul');
// => <ul class="lvl-2">...</ul>
Returns Element The closest matching descendant if found.
Check if an element is a descendant of another element.
Parameters
Examples
<div class=foo>
<div class=bar></div>
</div>
const foo = find(document, '.foo');
const bar = find(document, '.bar');
contains(foo, bar);
// => true
Returns boolean True if the child is a descendant of the parent.
Get or set the value of a CSS property of an element.
Parameters
element
Element The element whose CSS property to get or set.property
string The CSS property to get or set.value
string= The value of the CSS property if setting.
Examples
p {
color: red;
}
<p>Lorem ipsum</p>
const element = find(document, 'p');
css(element, 'color');
// => rgb(255, 0, 0)
css(element, 'color', 'blue');
<p style="color: blue;">Lorem ipsum</p>
Returns string The value of the CSS property if getting.
Get or set the value of a data attribute of an element.
Parameters
element
Element The element whose data attribute to get or set.key
string The key of the data attribute to get or set.value
string= The value of the data attribute if setting.
Examples
<p data-foo=bar>Lorem ipsum</p>
const element = find(document, 'p');
data(element, 'foo');
// => 'bar'
data(element, 'foo', 'baz')
<p data-foo=baz>Lorem ipsum</p>
Returns string The value of the data attribute if getting.
Remove all children (including text) from an element.
Parameters
element
Element The element whose children to remove.
Examples
<p>Lorem <b>ipsum</b></p>
const element = find(document, 'p');
empty(element);
<p></p>
Find the first element matching a query.
Parameters
scope
(Element|Document) The scope to look through.query
string The query to use for looking up the element.
Examples
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
find(document, 'li');
// => <li>Item 1</li>
Returns Element The element if found.
Find all elements matching a query.
Parameters
scope
(Element|Document) The scope to look through.query
string The query to use for looking up the elements.
Examples
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
findAll(document, 'li');
// => [<li>Item 1</li>, <li>Item 2</li>]
Returns Array.<Element> The elements if found.
Check if an element has a descendant matching a selector.
Parameters
Examples
<div class=foo>
<div class=bar></div>
</div>
const element = find(document, '.foo');
has(element, '.bar');
// => true
Returns boolean True if the element has a descendant matching the selector.
Check if an element has a class.
Parameters
Examples
<p class=foo>Lorem ipsum</p>
const element = find(document, 'p');
hasClass(element, 'foo');
// => true
Returns boolean True if the element has the class.
Get the computed height of a node.
Parameters
Examples
div {
padding: 10px 0 5px;
}
p {
line-height: 20px;
}
<div>
<p>Lorem ipsum</p>
</div>
const element = find(document, 'div');
height(element);
// => 35
Returns number The computed height of the node.
Get or set the inner HTML of an element.
Parameters
element
Element The element whose inner HTML to get or set.content
string The content of the inner HTML if setting.
Examples
<p>Lorem <b>ipsum</b></p>
const element = find(document, 'p');
html(element);
// => 'Lorem <b>ipsum</b>''
html(element, 'Dolor sit <b>amet</b>');
<p>Dolor sit <b>amet</b></p>
Returns string The inner HTML of the element if getting.
Check if an element matches a selector.
Parameters
Examples
<p class=foo>Lorem ipsum</p>
const element = find(document, 'p');
matches(element, 'div.foo');
// => true
Returns boolean True if the element matches the selector.
Get the next sibling of an element.
Parameters
element
Element The element whose sibling to get.
Examples
<p class=foo>Lorem ipsum</p>
<p>Dolor sit amet</p>
const element = find(document, '.foo');
next(element);
// => <p>Dolor sit amet</p>
Returns Element The sibling of the element if found.
Get the current coordinates of an element relative to its document
Parameters
element
Element The element whose coordinates to get.
Examples
div {
margin-lef: 10px;
line-height: 20px;
}
<div>Lorem ipsum</div>
<div class=foo>Dolor sit amet</div>
const element = find(document, '.foo');
offset(element);
// => {top: 20, left: 10}
Returns {top: number, left: number} The current coordinates of the element.
Get the parent of an element.
Parameters
element
Element The element whose parent to get.
Examples
<div>
<p>Lorem ipsum</p>
</div>
const element = find(document, 'p');
parent(element);
// => <div>...</div>
Returns Element The parent element if found.
Get all the parents of an element.
Parameters
element
Element The element whose parents to get.
Examples
<div>
<p>Lorem <b>ipsum</b></p>
</div>
const element = find(document, 'b');
parents(element);
// => [<p>...</p>, <div>...</div>]
Returns Array.<Element> The parents of the element.
Get the current coordinates of an element relative to its offset parent.
Parameters
element
Element The element whose coordinates to get.
Examples
div {
padding: 20px 10px;
}
<div>
<span>Lorem ipsum</span>
</div>
const element = find(document, 'span');
position(element);
// => {top: 20, left: 10}
Returns {top: number, left: number} The current coordinates of the element.
Insert HTML at the beginnig of an element.
Parameters
element
Element The element to insert the HTML at the beginning of.html
string The HTML to insert at the beginning of the element.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
append(element, '<b>Dolor sit amet</b>');
<p><b>Dolor sit amet</b>Lorem ipsum</p>
Get the previous sibling of an element.
Parameters
element
Element The element whose sibling to get.
Examples
<p>Lorem ipsum</p>
<p class=foo>Dolor sit amet</p>
const element = find(document, '.foo');
prev(element);
// => <p>Lorem ipsum</p>
Returns Element The sibling of the element if found.
Remove an element from its parent.
Parameters
element
Element The element to remove.
Examples
<p>Lorem <b>ipsum</b></p>
const element = find(document, 'b');
remove(element);
<p>Lorem </p>
Remove an attribute from an element.
Parameters
element
Element The element whose attribute to remove.name
string The name of the attribute to remove.
Examples
<img title="Lorem ipsum">
const element = find(document, 'img');
removeAttr(element, 'title');
<img>
Remove a class from an element.
Parameters
Examples
<p class="foo bar">Lorem ipsum</p>
const element = find(document, 'p');
removeClass(element, 'foo');
<p class=bar>Lorem ipsum</p>
Remove a data attribute from an element.
Parameters
element
Element The element whose data attribute to remove.key
string The key of the data attribute to remove.
Examples
<p data-foo=bar>Lorem ipsum</p>
const element = find(document, 'p');
removeData(element, 'foo');
<p>Lorem ipsum</p>
Replace an element with HTML.
Parameters
Examples
<p>Lorem <b>ipsum<b></p>
const element = find(document, 'b');
replace(element, '<i>ipsum</i>');
<p>Lorem <i>ipsum<i></p>
Get all the siblings of an element.
Parameters
element
Element The element whose siblings to get.
Examples
<ul>
<li>Item 1</li>
<li class=foo>Item 2</li>
<li>Item 3</li>
</ul>
const element = find(document, '.foo');
siblings(element);
// => [<li>Item 1</li>, <li>Item 2</li>]
Returns Array.<Element> The siblings of the element.
Get the computed style of an element.
Parameters
element
Element The element whose computed style to get.
Examples
p {
color: red;
}
<p style="float: right;">Lorem ipsum</p>
const element = find(document, 'p');
style(element);
// => CSSStyleDeclaration { color: 'rgb(255, 0, 0)', float: 'right', ... }
Returns CSSStyleDeclaration The computed style of the element.
Get the tag name of the element.
Parameters
element
Element The element whose tag name to get.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
tag(element);
// => 'p'
Returns string The tag name of the element.
Get or set the text content of an element.
Parameters
element
Element The element whose text content to get or set.content
string= The text content if setting.
Examples
<p>Lorem <b>ipsum</b></p>
const element = find(document, 'p');
text(element);
// => 'Lorem ipsum'
text(element, 'Lorem ipsum');
<p>Lorem ipsum</p>
Returns string The text content if getting.
Toggle a class on an element.
Parameters
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
toggleClass(element, 'foo');
<p class=foo>Lorem ipsum</p>
toggleClass(element, 'foo');
<p>Lorem ipsum</p>
Remove the parent of an element.
Parameters
element
Element The element whose parent to remove.
Examples
<div>
<p class=foo>Lorem ipsum</p>
<p>Dolor sit amet</p>
</div>
const element = find(document, '.foo');
unwrap(element);
<p class=foo>Lorem ipsum</p>
<p>Dolor sit amet</p>
Get or set the value of an element.
Parameters
element
Element The element whose value to get or set.value
string= The value of the element if setting.
Examples
<input value=foo></input>
const element = find(document, 'input');
val(element);
// => 'foo'
val(element, 'bar');
<input value=bar></input>
Returns string The value of the element if getting.
Get the computed width of a node.
Parameters
Examples
div {
padding: 0 10px;
}
p {
width: 40px;
}
<div>
<p>Lorem ipsum</p>
</div>
const element = find(document, 'div');
width(element);
// => 60
Returns number The computed width of the node.
Wrap an HTML structure around an element.
Parameters
element
Element The element to wrap the HTML structure around.html
string The HTML structure to wrap around the element.
Examples
<p>Lorem ipsum</p>
const element = find(document, 'p');
wrap(element, '<div></div>');
<div>
<p>Lorem ipsum</p>
</div>
Latest ✔ | Latest ✔ | 11+ ✔ | Latest ✔ | 6.1+ ✔ |
Copyright © 2016 Kasper Kronborg Isager. Licensed under the terms of the MIT license.