A lightweight jQuery alternative for modern browsers.
NPM:
npm install grindstone
Yarn:
yarn add grindstone
CDN:
<script type="text/javascript" src="https://unpkg.com/grindstone@latest/dist/grindstone.min.js"></script>
For the average project, 87kb or so worth of jQuery - minified - is unnecessary. Grindstone.js handles many commonly used methods while weighing in at just 13kb (minified).
This library does not aim to replace jQuery.
Grindstone.js supports the following browsers:
Browser | Version |
---|---|
Chrome | 4.0+ |
Firefox | 3.5+ |
Edge | 13+ |
IE | 10+ |
Safari | 3.2+ |
Opera | 10.0+ |
As is the case with jQuery, usage is as follows:
$(selector[, context]);
Extending Grindstone.js by adding new methods is as easy as well:
$.fn => Grindstone.prototype
$.fn.someNewMethod = function() {};
$(selector).someNewMethod();
Full documentation on all methods is below.
Ajax | Attributes | Collection | Events | Filtering | Forms | Manipulation | Miscellaneous | Traversing |
---|---|---|---|---|---|---|---|---|
ajax() | addClass() | each() | click() | filter() | submit() | after() | debounce() | children() |
attr() | eq() | doubleTap() | is() | val() | append() | extend() | contents() | |
data() | first() | focus() | not() | before() | mouseable() | next() | ||
hasAttr() | get() | load() | clone() | offset() | parent() | |||
hasClass() | last() | off() | css() | prev() | ||||
removeAttr() | map() | on() | empty() | |||||
removeClass() | ready() | height() | ||||||
removeData() | resize() | hide() | ||||||
toggleClass() | scroll() | html() | ||||||
scrollLeft() | prepend() | |||||||
scrollTop() | remove() | |||||||
trigger() | replaceWith() | |||||||
show() | ||||||||
width() | ||||||||
wrap() | ||||||||
wrapInner() |
Create an XMLHttpRequest.
Acceptable options include:
- method (GET, POST, PUT, etc.)
- url (data path)
- async (true or false)
- dataType (DOMString, blob, json, document, etc.)
- body (payload)
- headers (adds custom HTTP headers to the request)
$.ajax({
method: 'GET',
url: 'https://www.something.com/detail',
dataType: 'json'
})
.then((resp) => {})
.catch((err) => {});
$.ajax({
method: 'POST',
url: 'https://www.something.com/api',
body: { form: data },
headers: { 'Content-Type': 'application/json' }
})
.then((resp) => {})
.catch((err) => {});
Add a class or classes to the current set of elements.
$('#selector').addClass('example');
$('#selector').addClass('one two');
Set or return the value of the specified attribute.
$('#selector').addClass('example');
$('#selector').addClass('one two');
Trigger a callback on click, or trigger the click itself.
$('#selector').click();
$('#selector').click(() => {});
Assign a data-value attribute to a set of elements or return the current value of an element.
$('#selector').data('name');
$('#selector').data('name', 'value');
Determine if the current element has the specified attribute.
$('#selector').hasAttr('example');
Determine if the elements have the specified class(es).
$('#selector').hasClass('example');
$('#selector').hasClass('one two');
Remove the the specified attribute.
$('#selector').removeAttr('example');
Remove a class or classes from the current set of elements.
$('#selector').removeClass('example');
$('#selector').removeClass('one two');
Remove a data-value attribute from a set of elements.
$('#selector').removeData('name');
Toggle the specified class(es).
$('#selector').toggleClass('example');
$('#selector').toggleClass('one two');
Iterate through each item in the set and execute the callback.
$('.selector').each((item, index, array) => {});
Return the DOM element at the specified index of the current as a new instance of Grindstone.
$('.selector').eq(2);
Get the first element.
$('.selector').first();
Return the DOM element at the specified index of the current set.
$('.selector').get(2);
Get the last element.
$('.selector').last();
Map each element to an array of values.
$(array).map((item, index, array) => {});
Trigger a function by double-tapping or double-clicking.
$('#selector').doubleTap(() => {});
Focus on the first element in the set or trigger a callback when some element is focused on.
$('#selector').focus();
$('#selector').focus(() => {});
Trigger a function on the load event.
$(window).load(() => {});
Remove an event listener.
$('#selector').off('change', () => {});
$('#selector').off('click touchend', () => {});
Assign an event listener.
$('#selector').on('change', () => {});
$('#selector').on('click touchend', () => {});
Trigger a function when the DOM content is loaded.
$(document).ready(() => {});
Capture the resize event from a set of elements and execute a function.
$(window).resize(() => {});
Listen for the scroll event and trigger a function.
$(window).scroll(() => {});
Scroll an element to a specific left position relative to its another parent container.
Return the current left offset of an element, relative to its parent container.
$('#selector').scrollLeft();
$('#selector').scrollLeft(50);
Scroll an element to a specific top position relative to its another parent container.
Return the current top offset of an element, relative to its parent container.
$('#selector').scrollTop();
$('#selector').scrollTop(50);
Dispatch a custom event.
$('#selector').trigger('example');
Filter the elements by the selector or callback function.
$('.selector').filter('.visible');
Check if any of the elements match the given selector or callback function.
$('.selector').is('.visible');
Exclude matching elements and includes non-matching elements.
$('.selector').not('.hidden');
Submit a form or trigger a function when a form is submitted.
$('#selector').submit();
$('#selector').submit(() => {});
Return or assign the value of an element.
$('#selector').val();
$('#selector').val('7');
Insert new content after a target element.
$('#selector').after('<p>Hello World</p>');
Append a new element or new content.
$('#selector').append('#element');
$('#selector').append('<p>Hello World</p>');
Insert new content before a target element.
$('#selector').before('<p>Hello World</p>');
Clone the elements in the set.
$('#selector').clone();
Adjust the styles of selected elements or return the requested value.
$('#selector').style('display');
$('#selector').style('display', 'block');
$('#selector').style({ display: 'block', color: 'red' });
Remove all child nodes of all elements in the set.
$('.selector').empty();
Adjust the height of the selected elements or return the current height value of the first element in the set.
$('#selector').height();
$('#selector').height(30);
Hide a set of elements.
$('#selector').hide();
$('#selector').hide(100);
Replace an element's innerHTML or return the current innerHTML.
$('#selector').html();
$('#selector').html('<p>Hello World</p>');
Prepend a new element or new content.
$('#selector').prepend('#element');
$('#selector').prepend('<p>Hello World</p>');
Remove elements from the DOM.
$('#selector').remove();
$('#selector').remove('.selector');
Replace an element with some other content.
$('#selector').replaceWith('<p>Hello World</p>');
Show a set of hidden elements.
$('#selector').show();
$('#selector').show(100);
Adjust the width of the selected elements or return the current width value of the first element in the set.
$('#selector').width();
$('#selector').width(30);
Wrap the outer structure of the set of elements.
$('#selector').wrap('<div class="outer"><div class="inner">');
Wrap the inner structure of the set of elements.
$('#selector').wrapInner('<div class="outer"><div class="inner">');
Rate-limit a given function.
$.debounce(() => {}, 300);
Merge properties from one or more objects into a target object.
Existing properties in the target object will be overwritten if they exist in any of the argument objects.
$.extend({}, { foo: 'bar' });
$.extend(obj1, obj2, obj3, obj4);
Assign hover and active classes.
$('#selector').mouseable();
$('#selector').mouseable({ hoverClass: 'stuff', activeClass: 'things' });
Return the left or top value of the selector, relative to the document.
$('#selector').offset('left');
$('#selector').offset('top');
Get the child elements as a new collection.
$('#selector').children();
$('#selector').children('.selector');
Get all the children as a new collection, including text and comments.
$('#selector').contents();
Get the next element as a new collection.
$('#selector').next();
$('#selector').next('.selector');
Get the parent element as a new collection.
$('#selector').parent();
$('#selector').parent('.selector');
Get the previous element as a new collection.
$('#selector').prev();
$('#selector').prev('.selector');
npm install
npm start
npm run build
npm run docs
npm run lint
npm run lint:fix
npm test
npm run clean
npm run test:all
The runtime environment for this library requires
Node >= 13.6.0
andNPM >= 6.4.1
.
This library makes use of
ESLint
andEditorConfig
. Each of these features requires an extension be installed in order to work properly with IDEs and text editors such as VSCode.