domoperations
A library of helper methods for working with the DOM.
Installation
Simply do: npm install domoperations
.
What is it?
It's simply a collection of helper methods for working with the DOM such as create
, remove
and assignAttributes
.
Example
import {create, add, remove} from "domoperations";
let button = create("button", {disabled: true});
add(button);
remove(button);
Changelog:
v0.01:
Usage
Import it in your project like this:
import {create, ...} from "domoperations";
import {create, ...} from "domoperations/external";
import {create, ...} from "domoperations/native";
import {create, ...} from "domoperations/native-external";
import {create, ...} from "domoperations/typed";
Documentation
Types
interface PropertyObject {
[key: string]: any;
}
#nodeNameFromPrototype()
Extracts the node name for a prototype. For instance, calling the method with HTMLButtonElement will return 'button'.
Signature:
nodeNameFromPrototype (prototype: Class<HTMLElement>): string
Arguments:
prototype: Class<HTMLElement>
- The prototype to extract the name from.
Returns:
string
- The element name.
Throws:
-
TypeError
- If the prototype is 'HTMLElement'.
-
TypeError
- If the prototype is 'HTMLUnknownElement'.
-
TypeError
- If the name couldn't be decoded for some reason.
Example
nodeNameFromPrototype(HTMLButtonElement);
nodeNameFromPrototype(MyCustomElement);
#nodeNameFromInstance()
Extracts the node name for a instance of HTMLElement. For instance, an instance of HTMLButtonElement will return 'button'.
Signature:
nodeNameFromInstance (instance: HTMLElement): string
Arguments:
instance: HTMLElement
- The node instance to extract the name from.
Returns:
string
- The node name of the instance.
Throws:
TypeError
- If the given instance isn't of type 'HTMLElement'.
Example
nodeNameFromInstance(document.createElement("p"));
nodeNameFromInstance(document.createElement("my-custom-element"));
#remove()
Removes an element from the DOM.
Signature:
remove (element: HTMLElement): void
Arguments:
element: HTMLElement
- The element that should be removed.
Returns:
boolean
- Whether or not the removal were successful.
Throws:
TypeError
- If the given element isn't of type 'HTMLElement'.
Example
let paragraph = document.createElement("p");
document.appendChild(button);
remove(paragraph);
remove(document.createElement("p"));
#getFromTag()
Gets the element that matches the given tag name.
Signature:
getFromTag (tagName: string, all: boolean = false, root: Document|HTMLElement = document): HTMLCollection<*> | ?HTMLElement
Arguments:
-
tagName: string
- The tag name to find a matching element for.
-
all: boolean
- If true, all matches will be returned. default: false
-
root: Document|HTMLElement
- The root element of the query. default: document
Returns:
HTMLCollection<*>|?HTMLElement
- A HTMLCollection of matches if 'all' is true, otherwise the matched element, if any.
Throws:
-
TypeError
- If the first argument is not of type 'string'.
-
TypeError
- If the second argument is not of type 'boolean'.
-
TypeError
- If the third argument is not of type 'Document' or 'HTMLElement'
Example
getFromTag("head");
getFromTag("p", true);
#getFromClassName()
Gets the element that matches the given CSS class name.
Signature:
getFromClassName (className: string, all: boolean = false, root: Document|HTMLElement = document): HTMLCollection<*> | ?HTMLElement
Arguments:
-
className: string
- The CSS class name to find a matching element for.
-
all: boolean
- If true, all matches will be returned. default: false
-
root: Document|HTMLElement
- The root element of the query. default: document
Returns:
HTMLCollection<*>|?HTMLElement
- A HTMLCollection of matches if 'all' is true, otherwise the matched element, if any.
Throws:
-
TypeError
- If the first argument is not of type 'string'.
-
TypeError
- If the second argument is not of type 'boolean'.
-
TypeError
- If the third argument is not of type 'Document' or 'HTMLElement'
Example
getFromClassName("active");
getFromClassName("active", true);
#getFromId()
Gets the element that matches the given id.
Signature:
getFromId (id: string): ?HTMLElement
Arguments:
id: string
- The id to find a matching element for.
Returns:
?HTMLElement
- The matched element, if any.
Throws:
TypeError
- If the first argument is not of type 'string'.
Example
getFromId("container");
#addTo()
Adds the given element to another elements local DOM. If 'unique' is true, the element will only be added if no direct child exists with the same node name.
Signature:
addTo (to: HTMLElement, toAdd: HTMLElement, unique: boolean = false): Node
Arguments:
-
to: HTMLElement
- The element to add the element to.
-
toAdd: HTMLElement
- The element that should be added.
-
unique: boolean
- If true, the element will be added only if no direct child exists with the same node name. default: false
Returns:
HTMLElement
- The added element. If 'unique' is true and another element with the same node name exists already, that one will be returned.
Throws:
Example
addTo(document.body, document.createElement("p"));
addTo(document.body, document.createElement("p"), true);
#assignAttributes()
Sets the given attributes on the given element. Will automatically dash properties and handle booleans.
Signature:
assignAttributes (element: HTMLElement, attributes: PropertyObject): void
Arguments:
Returns:
void
Throws:
TypeError
- If the first argument isn't of type 'HTMLElement'.
Example
let button = document.createElement("button");
let customElement = dcoument.createElement("my-custom-element");
assignAttributes(button, {disabled: true});
assignAttributes(button, {disabled: false});
assignAttributes(customElement, {myCustomProp: "a string", anotherProp: 12})
#create()
Creates a new HTMLElement.
Signature:
create (elementName: string, properties?: PropertyObject, asAttributes: boolean = false): HTMLElement
Arguments:
elementName: string
- The name of the element to create.properties?: PropertyObject
- Any properties to set on the element upon creation.asAttributes: boolean
- if true, the properties will be set as attributes instead of as properties. default: false
Returns:
HTMLElement
- The created element .
Throws:
TypeError
- If the first argument isn't of type 'string'.TypeError
- If the third argument isn't of type 'boolean'.
Example
create("p");
create("button", {disabled: true}, true);
create("img", {src: "images/funnycatz.png"});