README
You Don't Need jQuery
Frontend environments evolve rapidly nowadays, modern browsers have already implemented a great deal of DOM/BOM APIs which are good enough. We don't have to learn jQuery from scratch for DOM manipulation or events. In the meantime, thanks to the prevailing of frontend libraries such as React, Angular and Vue, manipulating DOM directly becomes anti-pattern, jQuery has never been less important. This project summarizes most of the jQuery method alternatives in native implementation, with IE 10+ support.
Table of Contents
Translations
Query Selector
In place of common selectors like class, id or attribute we can use document.querySelector or document.querySelectorAll for substitution. The differences lie in:
document.querySelectorreturns the first matched elementdocument.querySelectorAllreturns all matched elements as NodeList. It can be converted to Array usingArray.prototype.slice.call(document.querySelectorAll(selector));If there are no elements matched, jQuery and
document.querySelectorAllwill return[], whereasdocument.querySelectorwill returnnull.
Notice:
document.querySelectoranddocument.querySelectorAllare quite SLOW, try to usedocument.getElementById,document.getElementsByClassNameordocument.getElementsByTagNameif you want to get a performance bonus.
1.0 Query by selector
1.1 Query by class
1.2 Query by id
1.3 Query by attribute
1.4 Query in descendants
1.5 Sibling/Previous/Next Elements
Sibling elements
Previous elements
Next elements
1.6 Closest
Return the first matched element by provided selector, traversing from current element to document.
1.7 Parents Until
Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector, DOM node, or jQuery object.
1.8 Form
Input/Textarea
Get index of e.currentTarget between
.radio
1.9 Iframe Contents
$('iframe').contents()returnscontentDocumentfor this specific iframeIframe contents
Iframe Query
1.10 Get body
1.11 Attribute getter and setter
Get an attribute
Set an attribute
Get a
data-attribute
CSS & Style
Add class
Remove class
has class
Toggle class
2.2 Width & Height
Width and Height are theoretically identical, take Height as example:
Window height
Document height
Element height
2.3 Position & Offset
Position
Get the current coordinates of the element relative to the offset parent.
Offset
Get the current coordinates of the element relative to the document.
2.4 Scroll Top
Get the current vertical position of the scroll bar for the element.
DOM Manipulation
3.1 Remove
Remove the element from the DOM.
3.2 Text
Get text
Get the combined text contents of the element including their descendants,
Set text
Set the content of the element to the specified text.
3.3 HTML
Get HTML
Set HTML
3.4 Append
Append child element after the last child of parent element
3.5 Prepend
3.6 insertBefore
Insert a new node before the selected elements
3.7 insertAfter
Insert a new node after the selected elements
3.8 is
Return
trueif it matches the query selector3.9 clone
Create a deep copy of that element
3.10 empty
Remove all child nodes
3.11 wrap
Wrap an HTML structure around each element
3.12 unwrap
Remove the parents of the set of matched elements from the DOM
3.13 replaceWith
Replace each element in the set of matched elements with the provided new content
3.14 simple parse
Parse a string into HTML/SVG/XML
Ajax
Fetch API is the new standard to replace XMLHttpRequest to do ajax. It works on Chrome and Firefox, you can use polyfills to make it work on legacy browsers.
Try github/fetch on IE9+ or fetch-ie8 on IE8+, fetch-jsonp to make JSONP requests.
4.1 Load data from the server and place the returned HTML into the matched element.
Events
For a complete replacement with namespace and delegation, refer to https://github.com/oneuijs/oui-dom-events
5.0 Document ready by
DOMContentLoaded5.1 Bind an event with on
5.2 Unbind an event with off
5.3 Trigger
Utilities
Most of utilities are found by native API. Others advanced functions could be chosen better utilities library focus on consistency and performance. Recommend lodash to replace.
6.1 Basic utilities
isArray
Determine whether the argument is an array.
isWindow
Determine whether the argument is a window.
inArray
Search for a specified value within an array and return its index (or -1 if not found).
isNumeric
Determine if the argument passed is numerical. Use
typeofto decide the type or thetypeexample for better accuracy.isFunction
Determine if the argument passed is a JavaScript function object.
isEmptyObject
Check to see if an object is empty (contains no enumerable properties).
isPlainObject
Check to see if an object is a plain object (created using “{}” or “new Object”).
extend
Merge the contents of two or more objects together into the first object. object.assign is ES6 API, and you could use polyfill also.
trim
Remove the white-space from the beginning and end of a string.
map
Translate all items in an array or object to new array of items.
each
A generic iterator function, which can be used to seamlessly iterate over both objects and arrays.
grep
Finds the elements of an array which satisfy a filter function.
type
Determine the internal JavaScript [Class] of an object.
merge
Merge the contents of two arrays together into the first array.
now
Return a number representing the current time.
proxy
Takes a function and returns a new one that will always have a particular context.
makeArray
Convert an array-like object into a true JavaScript array.
6.2 Contains
Check to see if a DOM element is a descendant of another DOM element.
6.3 Globaleval
Execute some JavaScript code globally.
6.4 parse
parseHTML
Parses a string into an array of DOM nodes.
parseJSON
Takes a well-formed JSON string and returns the resulting JavaScript value.
Promises
A promise represents the eventual result of an asynchronous operation. jQuery has its own way to handle promises. Native JavaScript implements a thin and minimal API to handle promises according to the Promises/A+ specification.
7.1 done, fail, always
doneis called when promise is resolved,failis called when promise is rejected,alwaysis called when promise is either resolved or rejected.7.2 when
whenis used to handle multiple promises. It will resolve when all promises are resolved, and reject if either one is rejected.7.3 Deferred
Deferred is a way to create promises.
Animation
8.1 Show & Hide
8.2 Toggle
Display or hide the element.
8.3 FadeIn & FadeOut
8.4 FadeTo
Adjust the opacity of the element.
8.5 FadeToggle
Display or hide the element by animating their opacity.
8.6 SlideUp & SlideDown
8.7 SlideToggle
Display or hide the element with a sliding motion.
8.8 Animate
Perform a custom animation of a set of CSS properties.
Alternatives
You Might Not Need jQuery - Examples of how to do common event, element, ajax etc with plain javascript.
npm-dom and webmodules - Organizations you can find individual DOM modules on NPM
Browser Support
![]()
![]()
![]()
![]()
![]()
Latest ✔
Latest ✔
10+ ✔
Latest ✔
6.1+ ✔
License
MIT
Last updated