README

You Don't Need jQuery

Build Status

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.querySelector returns the first matched element

  • document.querySelectorAll returns all matched elements as NodeList. It can be converted to Array using Array.prototype.slice.call(document.querySelectorAll(selector));

  • If there are no elements matched, jQuery and document.querySelectorAll will return [], whereas document.querySelector will return null.

Notice: document.querySelector and document.querySelectorAll are quite SLOW, try to use document.getElementById, document.getElementsByClassName or document.getElementsByTagName if 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() returns contentDocument for this specific iframe

    • Iframe contents

    • Iframe Query

  • 1.10 Get body

  • 1.11 Attribute getter and setter

    • Get an attribute

    • Set an attribute

    • Get a data- attribute

⬆ back to top

CSS & Style

  • 2.1 CSS

    • Get style

    • Set style

    • Get/Set Styles

      Note that if you want to set multiple styles once, you could refer to setStylesarrow-up-right method in oui-dom-utils package.

  • 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.

⬆ back to top

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 true if it matches the query selector

  • 3.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

⬆ back to top

Ajax

Fetch APIarrow-up-right 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/fetcharrow-up-right on IE9+ or fetch-ie8arrow-up-right on IE8+, fetch-jsonparrow-up-right to make JSONP requests.

  • 4.1 Load data from the server and place the returned HTML into the matched element.

⬆ back to top

Events

For a complete replacement with namespace and delegation, refer to https://github.com/oneuijs/oui-dom-eventsarrow-up-right

  • 5.0 Document ready by DOMContentLoaded

  • 5.1 Bind an event with on

  • 5.2 Unbind an event with off

  • 5.3 Trigger

⬆ back to top

Utilities

Most of utilities are found by native API. Others advanced functions could be chosen better utilities library focus on consistency and performance. Recommend lodasharrow-up-right 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 typeof to decide the type or the type example 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 polyfillarrow-up-right 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.

⬆ back to top

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+arrow-up-right specification.

  • 7.1 done, fail, always

    done is called when promise is resolved, fail is called when promise is rejected, always is called when promise is either resolved or rejected.

  • 7.2 when

    when is 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.

⬆ back to top

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

Browser Support

Latest ✔

Latest ✔

10+ ✔

Latest ✔

6.1+ ✔

License

MIT

Last updated