A simple lightweight library to make DOM querying and manipulation a little less laborious. The main purpose of this library is to save time from having to write out document.someKindOfFunction
every time you wish to interact with the DOM.
In order to add this functionality to a simple static webpage simply include the build file in the dist folder, or clone the repo and rebuild the files yourself.
To add the library to your npm project simply run npm i -D making-stuffs-queries
.
I will continue adding to this repo as, and when, I create new functions which I think are beneficial. If you have any suggestions or find any bugs please feel free to report them or make a pull request.
Anyway, here are some examples:
Replaces document.querySelector()
and takes two optional arguments:
-
A selector (typeof
string
), to be used as you would use the standard syntax fordocument.querySelector
. Defaults tobody
. -
An element (typeof
HTMLElement
), to be passed if you wish to query a specific element. Defaults todocument
.
const example = msQuery('.example');
const example = msQuery('#example');
const example = msQuery('[href="github.com"]');
const example = msQuery('div');
Replaces document.querySelectorAll()
and takes one optional argument:
- A selector (typeof
string
), to be used as you would with the standard syntax fordocument.querySelectorAll
. Defaults toa
.
const elemList = msQueryAll('.test');
Replaces the default document.createElement()
function and takes two optional arguments:
-
An element
tagName
(typeofstring
), used as you would use the standard syntax for thedocument.createElement()
function. Defaults todiv
. -
An object containing attributes in a key/value pair system (typeof
object
). To be used as if each key/value pair is being fed into thesetAttribute()
function.
If you would like to add hyphenated attributes to the created element, such as data attributes, you need to add them with an underscore. For example, if you wished to add data-index
attribute you would pass an object in the second argument with the following: { data_index: 'an index' }
.
If you would like to set some content for the newly created element you can do so by simply adding an innerHTML
key to the object passed for the second parameter. For example, to create a <h1></h1>
element with a title of Hey you would add the following to your options object: { innerHTML: 'Hey' }
.
const elem = msCreate(null, { class: 'test' });
Example 2: Create an anchor link with a href of 'github.com', data-role of 'button', class of 'test', and content of 'Anchor link'
const link = msCreate('a', { href: 'github.com', data_role: 'button', class: 'test', innerHTML: 'Anchor Link' });
Replaces the default document.append() and takes two arguments:
-
Either an element (typeof
HTMLElement
) or an array of elements which are to be appended to the parent. Required. -
An element (typeof
HTMLElement
) to which the provided child nodes will be appended. If no argument is provided the function will default todocument.body
msAppend([element1, element2]);
msAppend([element1, element2], parentElement);
msAppend(element);
msAppend(childElement, parentElement);