retroQuery is a JavaScript DOM interaction library inspired by jQuery. Using retroQuery, users can:
- Select single or multiple DOM elements
- Traverse and manipulate DOM elements
- Build DOM elements
- Create
DOMNodeCollection
objects fromHTMLElement
s - Queue functions until DOM is fully loaded
- Perform AJAX requests
To use retroQuery, download this repo and add rQuery.js
in your source code.
<script src="lib/rQuery.js" type="text/javascript"> </script>
- Iterates through the elements in a
DOMNodeCollection
and applies a callback function passed as an argument
this.each( element => {
element.each(childElement => {
element.appendChild(childElement.cloneNode(true));
})
- Returns collection of all children of each node.
- Returns a
DOMNodeCollection
of theparent
s of each of the nodes.
- Returns the
innerHTML
for the first element in ntheDOMNodeCollection
if no argument is given. If a string argument is given, changes theinnerHTML
of eachDOMNodeCollection
element to the string argument.
- Empties the innerHTML of each
DOMNodeCollection
element
- Takes a single
HTMLElement
,DOMNodeCollection
, orstring
argument and appends it to eachDOMNodeCollection
element.
- This method clears out the content of all nodes in the internal array.
- Takes either one (
attr(key)
) or two (attr(key, value)
) arguments. If given one argument, the method gets the value of the attribute given for the the first element in theDOMNodeCollection
. The method sets the attribute, given as the first argument, as the value, given as the second argument, for eachDOMNodeCollection
element.
- Returns a
DOMNodeCollection
of all the nodes matching the selector passed in as an argument that are descendants of the nodes.
- Adds className to the DOM elements' class properties
- Removes className from the DOM elements' class properties
- Toggles a class, given as an argument, for each
DOMNodeCollection
element.
- Adds event listener to each
DOMNodeCollection
element.
$r(".removeButton").on('click', (e) => {
$r(e.currentTarget).parent().remove();
});
- Removes event listener from each
DOMNodeCollection
element.
Sends HTTP Request and returns a Promise
object. Accepts a Hash
object as an argument with any of the following attributes:
- method (default: "GET"): HTTP Request method or type
- url (default: window.location.href): URL for HTTP Request
- success: success callback
- error: error callback
- contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8'): content type of HTTP Request
$r.ajax = options => {
const request = new XMLHttpRequest();
const defaults = {
contentType: 'application/x-www.form-urlencoded; charset=UTF-8',
method: 'GET',
url:'',
success: () => {},
errors: () => {},
data: {}
};
options = $r.extend(defaults, options);
options.method = options.method.toUpperCase();
request.open(options.method, options.url, true);
request.onload = e => {
if (request.status === 200) {
options.success(request.response);
} else {
options.error(request.response);
}
};
request.send(JSON.stringify(options.data));
};