freeDOM is a lightweight JavaScript DOM interaction library inspired by jQuery that enables users to:
- traverse and manipulate DOM elements
- handle events through
on
andoff
methods - queue functions until document is ready
- submit AJAX requests
freeDOM's $l
serves as a wrapper for all of the library's methods and provides the following functionality:
- select elements with CSS selectors
- create
DOMNodeCollection
objects - queue functions to be run once document is ready
const $l = arg => {
let nodesList;
if (typeof(arg) === 'string') {
const nodes = document.querySelectorAll(arg);
nodesList = Array.from(nodes);
} else if (arg instanceof HTMLElement) {
nodesList = [arg];
} else if (typeof(arg) === 'function') {
return docReady ? arg() : docReadyCallbacks.push(arg);
}
return new DOMNodeCollection(nodesList);
};
-
html(html)
takes in a string as argument to setinnerHTML
of selected elements. However, the return value is theinnerHTML
of the first element if no argument is provided. -
empty()
removes theinnerHTML
of all selected elements. -
append(arg)
checks the passed in argument and appends either anHTMLElement
, string,DOMNodeCollection
to every selected element. -
attr(key, value)
will set the attribute of the passed inkey
andvalue
to each selected element if the value is the string. Otherwise, the function will return the attribute the first element. -
addClass(className)
adds class, passed in as argument, to all selected elements. -
removeClass(className)
removes the specified class, passed in as argument, from each selected element.
-
each
iterates through the nodes of aDOMNodeCollection
and applies a callback function. -
children()
returnsDOMNodeCollection
of all direct children elements of every selected element. -
parent()
returnsDOMNodeCollection
of all parent elements of every selected element. -
find(selector)
returns all elements matching the passed in CSS selector argument. -
remove()
removes each element from DOM.
-
on
adds an event listener to each selected elements. -
off
removes event listener from each selected elements.
freeDOM offers the functionality to send asynchronous HTTP requests.
$l.ajax({options})
takes aHash
object as argument with multiple attributes:
$l.ajax({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/comments',
data: { postId: '1' },
})