jQuery
Abbreviations
*[API]: Application Programming Interface
*[CSS]: Cascading Style Sheets
*[DOM]: Document Object Model
*[HTTP]: Hypertext Transfer Protocol
*[JS]: JavaScript
Introduction
This unit will cover how to use one of the most popular libraries in web development: jQuery. Libraries provide additional functionality outside of what is built-in the language. They provide new functionality for you to use without having to implement the functions and methods yourself. jQuery is a JavaScript library that provides handy tools fro DOM manipulation. This library is extremely pervasive and relatively easy to use.
In this subunit, we'll teach you about jQuery. jQuery is a JavaScript library designed to make DOM manipulation more accessible and less tedious. It's very user-friendly! jQuery was once the most popular JavaScript library and is still used at three out of every four companies doing work in JavaScript. There's tons of documentation for jQuery beyond what we're going to teach you in this subunit, and we encourage you to explore some of the fun animations and other features of this library on your own time.
Although it's not as popular as it once was, jQuery is still used by 77% of the top 1 million websites. jQuery will make your life easier and you'll use it at work all the time. Fortunately, you already have a strong foundation in DOM manipulation, so learning jQuery will be a breeze. You'll see how to select, modify, and create elements, attach event listeners, and some other great DOM operations.
Goals
Develop a conceptual understanding of jQuery and its methods
Explain what you would or would not use a library like jQuery
Compare and contrast jQuery with [[javascript.vanilla]]
jQuery
What is jQuery
It's a library for:
Manipulating the DOM
Adding Event Listeners
Animating Elements
Making HTTP Requests (AJAX)
Why should you still learn jQuery
Brevity and clarity
Cross-Browser Support
AJAX
77% of the top 1,000,000 most visited pages use it
Including jQuery and Selecting Elements
Including jQuery
Once you include jQuery script, you have access to global $
Selecting elements
It's as easy as using CSS selectors! (except you need to remember your CSS selectors)
What does this give you
A jQuery object
jQuery objects are NOT the same as DOM elements
To access an element, use the get function:
Storing jQuery Objects in variables
It's a common convention to store jQuery objects in variable names that begin with $.
This isn't necessary, but clearly communicates your intent.
jQuery Methods
Common jQuery Methods
A great way to learn these are to compare them to vanilla JS methods
.val()
.text()
.attr()
.html()
.css()
.addClass() .removeClass() .toggleClass()
.empty() .remove()
.append() .prepend()
.find() .closest() .parent() .next() .prev()
jQuery getter / setter pattern
Vanilla JS:
.getAttribute(attrName)
and.setAttribute(attrName, newValue)
jQuery:
.attr(attrName, [newValue])
(second param is optional)This is common with jQuery methods
Chaining with jQuery
Almost all jQuery methods return a jQuery object, which allows for method chaining.
Instead of performing DOM operations line-by-line, we can chain method calls together on a single jQuery object.
Instead of:
We can have:
Creating elements
Instead of using document.createElement("li")
we can simply create an element using $("<li>")
$("<li>")
creates a new li$("li")
selects existing li
Waiting for the DOM to load
With vanilla JS we have DOMContentLoaded and window.onload, with jQuery we have:
You may see this version:
Events and Delegation with jQuery
jQuery events
jQuery's on() works similarly to addEventListener. It lets you specify the type of event to listen for.
Why Use on()
In most cases, click() and on("click") will both get the job done. HOWEVER, there is one key difference:
.click(callback) is a shorthand for .on("click", callback)
on() accepts optional argument between type of event and callback
This flexibility allows us to leverage event delegation.
Event Delegation
Event delegation allows us to attach an event listener to a parent element, but only invoke the callback if the event target matches a certain selector.
This will work even if elements matching the selector don't exist yet!
less code
more performant
Event Delegation: Vanilla JS vs. jQuery
Vanilla JS
jQuery
Wrap Up
Why might you not use jQuery
The DOM API is much more standardized
It doesn't do anything you can’t do on your own
It's an unnecessary dependency