In this article, we're focusing on jQuery. We're going to take a high-level look at everything that jQuery offers and how it can help us, and we'll review whether it is still relevant today.
First released in 2006, jQuery set out to be a cross-platform JavaScript library that made it easier to write client-side solutions.
At the time this was released, it was especially useful because of the inconsistencies that existed among JavaScript implementations in Internet Explorer, Firefox, and eventually Google Chrome (which wasn't released until 2008).
The jQuery website itself says:
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.
But what does this mean for us as developers? Perhaps the best way for us to understand what the library offers is to examine what it claims to offer.
When a browser renders a web page, it's a visual representation of what's known as the DOM (or the document object model). This model can be conceptually modeled as a tree data structure where there are certain nodes, each with roots and leaves.
For example, see this image as provided by the Web Step Book:
When you're working with jQuery, you can easily traverse the contents of the DOM in order to reach or to find the nodes, elements, or values you're aiming to retrieve.
This means that if you're looking for the text of a div
element that has a unique ID, it's easy to do.
/** * This code looks for a div with the ID of "my unique element" * and then hides it from view. */ $( 'div#my-unique-element' ).hide();
If you're trying to loop through all of the span
elements, you can do that as well:
/** * This is the basic way to set up a loop in jQuery. It will * take all of the span elements on the page and then * allow you to iterate through them. */ $( 'span' ).each(function() { // Process the span element here });
We'll review this particular functionality a little bit more in the next section as it goes to show some of the additional work you can do to manipulate the page.
Granted, these examples are simple, and things can get more complicated, especially as we introduce method chaining.
Suffice it to say, the power of jQuery lies in its ability to query the DOM (hence the name jQuery) and then make adjustments to it through the use of a well-documented API (that's replete with examples of how to use each function).
One could argue that everything else stems from that feature alone. So with that said, let's continue by looking at HTML document manipulation.
When it comes to actually manipulating the DOM, jQuery has a lot of functions that allow us to change what our visitors see.
Some of these functions are simple, such as allowing us to show
or hide
elements that aren't visible whenever a page loads. Other functions allow us to create new elements and append
them to an existing element, or prepend
them in front of an entire list.
For example, if you're trying to loop through all of the span
elements in order to add a class attribute to them, you can do that as follows:
/** * This is the basic way to set up a loop in jQuery. It will * take all of the span elements on the page and then * add a custom class attribute to them. */ $( 'span' ).each(function() { $( this ).addClass( 'my-custom-class' ); });
This is barely scratching the surface of what DOM manipulation functionality is available within jQuery. By looking at the API, under the Manipulation section, you can see just how many options are available to us (along with good examples).
To give further examples, we can also:
Remember that one of the things that make jQuery an attractive solution for so many developers is that all of the functions and examples we're looking at in this article are cross-browser compatible.
If you're brand new to JavaScript, then one thing that's key to understanding how it works with the page that's being displayed in the web browser is that it responds to various events.
That is, when a user clicks on an element, makes a keystroke, or clicks the mouse, the browser raises a signal corresponding to the event that occurred. This is what allows us to take advantage of the user's interaction with the browser.
Specifically, every time a user does something to the page, we can respond using a custom event. The problem is that not every browser implements events the same way (this is why there's a need for a specification, but that's a topic for another post).
Luckily, jQuery makes event handling much easier by defining a consistent name for all of the events such that we can use the same name for an event to which we're trying to respond, and it will work across all of the major browsers.
When jQuery first came out, Flash was still relatively popular, and general animations across the web weren't completely dead.
When we talk about animation in the context of jQuery, though, we're not talking about the same types of effects or behaviors that we're used to seeing with older technology. Instead, we're talking about effects that give users feedback that something has happened on the screen. Furthermore, it's less invasive and can add a nice sense of style to a page or application when used correctly (anything can be abused, though).
You can view the entire effects API on this page, but it's worth noting that jQuery's effects can range from handling simple fading in and fading out of elements or sliding elements into view to something more complex such as manipulating the queue of effects that are registered to fire against an element.
Granted, the latter case assumes you have a bit of experience with the effects API, but it's something that comes naturally given enough time with the library and the documentation.
If you're not familiar with AJAX, it's essentially a way that a web page can make a call to the server, handle the response, and update a portion of a page without having to refresh the entire page. This is the basic technique used for integrating data from a REST API in your app.
One of the main use cases for jQuery in the past was to simplify AJAX calls. However, improvements to the web API and browser compatibility have made jQuery less necessary for this. If you want to explore an alternative to using jQuery for AJAX and API calls, check out the Fetch API. We also have a free video tutorial on using the Fetch API.
With jQuery, it's easy to handle GET
and POST
requests while also having the ability to make far more advanced calls using the $.ajax
method.
One feature that a lot of server-side frameworks and libraries offer is the ability to create extensions to the core codebase. Modern client-side libraries and frameworks allow this, and jQuery is no different.
Say, for example, you work in a particular niche in which you find yourself creating the same functionality for each project. Or what if you have a product that you're selling and you have a bit of custom code that needs to integrate with jQuery, but it might require different parameters depending on the project.
What do you do then?
Fortunately, jQuery supports plugins. This means that we, as developers, not only have the ability to tap into plugins that others have written (some of which are available on the jQuery website, others being available on GitHub), but we're also able to develop our own plugins.
We can then reuse this code in our own projects or make the plugins available on sites such as GitHub for others to offer contributions, fixes, features, and so on.
From the jQuery UI homepage:
jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.
This library was first published in 2007, about a year after jQuery itself. It works as a complementary library to jQuery in that it leverages the cross-platform compatibility of the library to help create widgets that can be used throughout a website.
Many of the widgets include commonly used pieces of functionality. For example:
There are also advanced features such as effects, utilities, and interactions. All the features that we've covered so far (as well as the things we haven't) include a wide variety of callbacks, attributes, and functions that allow us to interact with them to the fullest extent.
All of the aforementioned features also offer various themes to make sure they fit the look and feel of your website. Finally, all of the features outlined here and included on the site are well documented.
When learning more about jQuery, you will come across articles that will tell you that jQuery is not needed today. However, you should take that argument with a pinch of salt.
It is certainly true that a lot of people now use modern web browsers, so jQuery is no longer as important to maintain cross-browser compatibility as it was before. However, you should always consider the browsers that your potential viewers would be using before deciding to ditch the library. Some users may have technology constraints that don't allow them to use the most modern browser.
Another argument against using jQuery is that the web standards have improved and it is a lot easier to do certain things with pure JavaScript now than it was in the past. This is also true, and you no longer need to use jQuery while writing code for simple projects and applications.
The primary reason to still use jQuery today is that you will have to write less code to get the same thing done. It will basically cut down your development time. Let's say you want to add, remove, or toggle some classes for the following elements in your DOM.
<h1 class="common extra">This is a heading.</h1> <h2 class="common switched">This is a subheading.</h2> <h3 class="common extra">This is a level 3 heading.</h3> <p class="common switched">This is a paragraph.</p>
Here is the vanilla JavaScript that you would have to use:
let common_elements = document.querySelectorAll(".common"); common_elements.forEach(function(elem) { elem.classList.add("new"); elem.classList.remove("extra"); elem.classList.toggle("switched"); });
This is much more straightforward than it was at the time jQuery was created, but it's still more verbose than the equivalent in jQuery.
The same thing can be done in jQuery with a single line of code:
$(".common").addClass("new").removeClass("extra").toggleClass("switched");
This is just one example, but you can see that jQuery will still save you a lot of time when writing code. The method chaining and easier event management in jQuery are big time-saving features.
If this is all the DOM manipulation that you need to do in a project, then I certainly don't recommend using jQuery. However, the lines that you need to write will add up as your projects get bigger. That being said, as a well-rounded developer you should always learn how to do these things in pure JavaScript as well.
Sometimes, you will need to use jQuery in a project not because you want to but because the project depends on it. One great example of this is WordPress. Yes, the most popular CMS in the world relies on jQuery. WordPress will probably move away from jQuery in the future, but for the time being, you should still learn how to work with the library.
Another reason that you might still need to use jQuery in a project is the extensive plugin system of the library. jQuery has been around for a long time now, and its popularity means that a lot of developers created some very useful plugins for adding functionality to websites. These plugins themselves will require jQuery, so you have no choice but to include the library in your projects.
Things are somewhat different when it comes to new projects that you will develop from scratch. The scope of requirements that we expect the front end to handle has expanded over the last decade. A lot of new and better-suited libraries and frameworks like Vue, React, and Angular have popped up to help you there. However, that topic is beyond the scope of this article.
Understanding what jQuery is (and what it isn't) and how it's related to JavaScript is important so that you know what's being done for you and what you can do when needing to work with the library.
As previously mentioned, some may argue that you need to learn JavaScript first and then learn jQuery; others may argue that learning jQuery is a great way to work your way backwards to JavaScript.
Whatever the case, jQuery is a longstanding library in the JavaScript economy and is one that's used in a number of very popular projects (such as WordPress), so learning it will give you a leg up in a number of different ways.
JavaScript has become one of the de facto languages of working on the web. It’s not without its learning curves, and there are plenty of frameworks and libraries to keep you busy. If you’re looking for additional resources to study or to use in your work, check out what we have available on CodeCanyon.
If that's not enough, there's plenty of documentation and open-source code available for you to review and read as well. There are also widely available plugins and an active blog to keep you in the loop with all of the news happening with the library's development.
The Best Small Business Web Designs by DesignRush
/Create Modern Vue Apps Using Create-Vue and Vite
/How to Fix the “There Has Been a Critical Error in Your Website” Error in WordPress
How To Fix The “There Has Been A Critical Error in Your Website” Error in WordPress
/How Long Does It Take to Learn JavaScript?
/The Best Way to Deep Copy an Object in JavaScript
/Adding and Removing Elements From Arrays in JavaScript
/Create a JavaScript AJAX Post Request: With and Without jQuery
/5 Real-Life Uses for the JavaScript reduce() Method
/How to Enable or Disable a Button With JavaScript: jQuery vs. Vanilla
/How to Enable or Disable a Button With JavaScript: jQuery vs Vanilla
/Confirm Yes or No With JavaScript
/How to Change the URL in JavaScript: Redirecting
/15+ Best WordPress Twitter Widgets
/27 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/21 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/30 HTML Best Practices for Beginners
/31 Best WordPress Calendar Plugins and Widgets (With 5 Free Plugins)
/25 Ridiculously Impressive HTML5 Canvas Experiments
/How to Implement Email Verification for New Members
/How to Create a Simple Web-Based Chat Application
/30 Popular WordPress User Interface Elements
/Top 18 Best Practices for Writing Super Readable Code
/Best Affiliate WooCommerce Plugins Compared
/18 Best WordPress Star Rating Plugins
/10+ Best WordPress Twitter Widgets
/20+ Best WordPress Booking and Reservation Plugins
/Working With Tables in React: Part Two
/Best CSS Animations and Effects on CodeCanyon
/30 CSS Best Practices for Beginners
/How to Create a Custom WordPress Plugin From Scratch
/10 Best Responsive HTML5 Sliders for Images and Text… and 3 Free Options
/16 Best Tab and Accordion Widget Plugins for WordPress
/18 Best WordPress Membership Plugins and 5 Free Plugins
/25 Best WooCommerce Plugins for Products, Pricing, Payments and More
/10 Best WordPress Twitter Widgets
1 /12 Best Contact Form PHP Scripts for 2020
/20 Popular WordPress User Interface Elements
/10 Best WordPress Star Rating Plugins
/12 Best CSS Animations on CodeCanyon
/12 Best WordPress Booking and Reservation Plugins
/12 Elegant CSS Pricing Tables for Your Latest Web Project
/24 Best WordPress Form Plugins for 2020
/14 Best PHP Event Calendar and Booking Scripts
/Create a Blog for Each Category or Department in Your WooCommerce Store
/8 Best WordPress Booking and Reservation Plugins
/Best Exit Popups for WordPress Compared
/Best Exit Popups for WordPress Compared
/11 Best Tab & Accordion WordPress Widgets & Plugins
/12 Best Tab & Accordion WordPress Widgets & Plugins
1New Course: Practical React Fundamentals
/Preview Our New Course on Angular Material
/Build Your Own CAPTCHA and Contact Form in PHP
/Object-Oriented PHP With Classes and Objects
/Best Practices for ARIA Implementation
/Accessible Apps: Barriers to Access and Getting Started With Accessibility
/Dramatically Speed Up Your React Front-End App Using Lazy Loading
/15 Best Modern JavaScript Admin Templates for React, Angular, and Vue.js
/15 Best Modern JavaScript Admin Templates for React, Angular and Vue.js
/19 Best JavaScript Admin Templates for React, Angular, and Vue.js
/New Course: Build an App With JavaScript and the MEAN Stack
/Hands-on With ARIA: Accessibility Recipes for Web Apps
/10 Best WordPress Facebook Widgets
13 /Hands-on With ARIA: Accessibility for eCommerce
/New eBooks Available for Subscribers
/Hands-on With ARIA: Homepage Elements and Standard Navigation
/Site Accessibility: Getting Started With ARIA
/How Secure Are Your JavaScript Open-Source Dependencies?
/New Course: Secure Your WordPress Site With SSL
/Testing Components in React Using Jest and Enzyme
/Testing Components in React Using Jest: The Basics
/15 Best PHP Event Calendar and Booking Scripts
/Create Interactive Gradient Animations Using Granim.js
/How to Build Complex, Large-Scale Vue.js Apps With Vuex
1 /Examples of Dependency Injection in PHP With Symfony Components
/Set Up Routing in PHP Applications Using the Symfony Routing Component
1 /A Beginner’s Guide to Regular Expressions in JavaScript
/Introduction to Popmotion: Custom Animation Scrubber
/Introduction to Popmotion: Pointers and Physics
/New Course: Connect to a Database With Laravel’s Eloquent ORM
/How to Create a Custom Settings Panel in WooCommerce
/Building the DOM faster: speculative parsing, async, defer and preload
1 /20 Useful PHP Scripts Available on CodeCanyon
3 /How to Find and Fix Poor Page Load Times With Raygun
/Introduction to the Stimulus Framework
/Single-Page React Applications With the React-Router and React-Transition-Group Modules
12 Best Contact Form PHP Scripts
1 /Getting Started With the Mojs Animation Library: The ShapeSwirl and Stagger Modules
/Getting Started With the Mojs Animation Library: The Shape Module
/Getting Started With the Mojs Animation Library: The HTML Module
/Project Management Considerations for Your WordPress Project
/8 Things That Make Jest the Best React Testing Framework
/Creating an Image Editor Using CamanJS: Layers, Blend Modes, and Events
/New Short Course: Code a Front-End App With GraphQL and React
/Creating an Image Editor Using CamanJS: Applying Basic Filters
/Creating an Image Editor Using CamanJS: Creating Custom Filters and Blend Modes
/Modern Web Scraping With BeautifulSoup and Selenium
/Challenge: Create a To-Do List in React
1Deploy PHP Web Applications Using Laravel Forge
/Getting Started With the Mojs Animation Library: The Burst Module
/10 Things Men Can Do to Support Women in Tech
/A Gentle Introduction to Higher-Order Components in React: Best Practices
/Challenge: Build a React Component
/A Gentle Introduction to HOC in React: Learn by Example
/A Gentle Introduction to Higher-Order Components in React
/Creating Pretty Popup Messages Using SweetAlert2
/Creating Stylish and Responsive Progress Bars Using ProgressBar.js
/18 Best Contact Form PHP Scripts for 2022
/How to Make a Real-Time Sports Application Using Node.js
/Creating a Blogging App Using Angular & MongoDB: Delete Post
/Set Up an OAuth2 Server Using Passport in Laravel
/Creating a Blogging App Using Angular & MongoDB: Edit Post
/Creating a Blogging App Using Angular & MongoDB: Add Post
/Introduction to Mocking in Python
/Creating a Blogging App Using Angular & MongoDB: Show Post
/Creating a Blogging App Using Angular & MongoDB: Home
/Creating a Blogging App Using Angular & MongoDB: Login
/Creating Your First Angular App: Implement Routing
/Persisted WordPress Admin Notices: Part 4
/Creating Your First Angular App: Components, Part 2
/Persisted WordPress Admin Notices: Part 3
/Creating Your First Angular App: Components, Part 1
/How Laravel Broadcasting Works
/Persisted WordPress Admin Notices: Part 2
/Create Your First Angular App: Storing and Accessing Data
/Persisted WordPress Admin Notices: Part 1
/Error and Performance Monitoring for Web & Mobile Apps Using Raygun
/Using Luxon for Date and Time in JavaScript
7 /How to Create an Audio Oscillator With the Web Audio API
/How to Cache Using Redis in Django Applications
/20 Essential WordPress Utilities to Manage Your Site
/Introduction to API Calls With React and Axios
/Beginner’s Guide to Angular 4: HTTP
/Rapid Web Deployment for Laravel With GitHub, Linode, and RunCloud.io
/Beginners Guide to Angular 4: Routing
/Beginner’s Guide to Angular 4: Services
/Beginner’s Guide to Angular 4: Components
/Creating a Drop-Down Menu for Mobile Pages
/Introduction to Forms in Angular 4: Writing Custom Form Validators
/10 Best WordPress Booking & Reservation Plugins
/Getting Started With Redux: Connecting Redux With React
/Getting Started With Redux: Learn by Example
/Getting Started With Redux: Why Redux?
/How to Auto Update WordPress Salts
/How to Download Files in Python
/Eloquent Mutators and Accessors in Laravel
1 /10 Best HTML5 Sliders for Images and Text
/Site Authentication in Node.js: User Signup
/Creating a Task Manager App Using Ionic: Part 2
/Creating a Task Manager App Using Ionic: Part 1
/Introduction to Forms in Angular 4: Reactive Forms
/Introduction to Forms in Angular 4: Template-Driven Forms
/24 Essential WordPress Utilities to Manage Your Site
/25 Essential WordPress Utilities to Manage Your Site
/Get Rid of Bugs Quickly Using BugReplay
1 /Manipulating HTML5 Canvas Using Konva: Part 1, Getting Started
/10 Must-See Easy Digital Downloads Extensions for Your WordPress Site
/22 Best WordPress Booking and Reservation Plugins
/Understanding ExpressJS Routing
/15 Best WordPress Star Rating Plugins
/Creating Your First Angular App: Basics
/Inheritance and Extending Objects With JavaScript
/Introduction to the CSS Grid Layout With Examples
1Performant Animations Using KUTE.js: Part 5, Easing Functions and Attributes
Performant Animations Using KUTE.js: Part 4, Animating Text
/Performant Animations Using KUTE.js: Part 3, Animating SVG
/New Course: Code a Quiz App With Vue.js
/Performant Animations Using KUTE.js: Part 2, Animating CSS Properties
Performant Animations Using KUTE.js: Part 1, Getting Started
/10 Best Responsive HTML5 Sliders for Images and Text (Plus 3 Free Options)
/Single-Page Applications With ngRoute and ngAnimate in AngularJS
/Deferring Tasks in Laravel Using Queues
/Site Authentication in Node.js: User Signup and Login
/Working With Tables in React, Part Two
/Working With Tables in React, Part One
/How to Set Up a Scalable, E-Commerce-Ready WordPress Site Using ClusterCS
/New Course on WordPress Conditional Tags
/TypeScript for Beginners, Part 5: Generics
/Building With Vue.js 2 and Firebase
6 /Best Unique Bootstrap JavaScript Plugins
/Essential JavaScript Libraries and Frameworks You Should Know About
/Vue.js Crash Course: Create a Simple Blog Using Vue.js
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 5.5 API
/API Authentication With Node.js
/Beginner’s Guide to Angular: HTTP
/Beginner’s Guide to Angular: Routing
/Beginners Guide to Angular: Routing
/Beginner’s Guide to Angular: Services
/Beginner’s Guide to Angular: Components
/How to Create a Custom Authentication Guard in Laravel
/Learn Computer Science With JavaScript: Part 3, Loops
/Build Web Applications Using Node.js
/Learn Computer Science With JavaScript: Part 4, Functions
/Learn Computer Science With JavaScript: Part 2, Conditionals
/Create Interactive Charts Using Plotly.js, Part 5: Pie and Gauge Charts
/Create Interactive Charts Using Plotly.js, Part 4: Bubble and Dot Charts
Create Interactive Charts Using Plotly.js, Part 3: Bar Charts
/Awesome JavaScript Libraries and Frameworks You Should Know About
/Create Interactive Charts Using Plotly.js, Part 2: Line Charts
/Bulk Import a CSV File Into MongoDB Using Mongoose With Node.js
/Build a To-Do API With Node, Express, and MongoDB
/Getting Started With End-to-End Testing in Angular Using Protractor
/TypeScript for Beginners, Part 4: Classes
/Object-Oriented Programming With JavaScript
/10 Best Affiliate WooCommerce Plugins Compared
/Stateful vs. Stateless Functional Components in React
/Make Your JavaScript Code Robust With Flow
/Build a To-Do API With Node and Restify
/Testing Components in Angular Using Jasmine: Part 2, Services
/Testing Components in Angular Using Jasmine: Part 1
/Creating a Blogging App Using React, Part 6: Tags
/React Crash Course for Beginners, Part 3
/React Crash Course for Beginners, Part 2
/React Crash Course for Beginners, Part 1
/Set Up a React Environment, Part 4
1 /Set Up a React Environment, Part 3
/New Course: Get Started With Phoenix
/Set Up a React Environment, Part 2
/Set Up a React Environment, Part 1
/Command Line Basics and Useful Tricks With the Terminal
/How to Create a Real-Time Feed Using Phoenix and React
/Build a React App With a Laravel Back End: Part 2, React
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 9 API
/Creating a Blogging App Using React, Part 5: Profile Page
/Pagination in CodeIgniter: The Complete Guide
/JavaScript-Based Animations Using Anime.js, Part 4: Callbacks, Easings, and SVG
/JavaScript-Based Animations Using Anime.js, Part 3: Values, Timeline, and Playback
/Learn to Code With JavaScript: Part 1, The Basics
/10 Elegant CSS Pricing Tables for Your Latest Web Project
/Getting Started With the Flux Architecture in React
/Getting Started With Matter.js: The Composites and Composite Modules
Getting Started With Matter.js: The Engine and World Modules
/10 More Popular HTML5 Projects for You to Use and Study
/Understand the Basics of Laravel Middleware
/Iterating Fast With Django & Heroku
/Creating a Blogging App Using React, Part 4: Update & Delete Posts
/Creating a jQuery Plugin for Long Shadow Design
/How to Register & Use Laravel Service Providers
2 /Unit Testing in React: Shallow vs. Static Testing
/Creating a Blogging App Using React, Part 3: Add & Display Post
/Creating a Blogging App Using React, Part 2: User Sign-Up
20 /Creating a Blogging App Using React, Part 1: User Sign-In
/Creating a Grocery List Manager Using Angular, Part 2: Managing Items
/9 Elegant CSS Pricing Tables for Your Latest Web Project
/Dynamic Page Templates in WordPress, Part 3
/Angular vs. React: 7 Key Features Compared
/Creating a Grocery List Manager Using Angular, Part 1: Add & Display Items
New eBooks Available for Subscribers in June 2017
/Create Interactive Charts Using Plotly.js, Part 1: Getting Started
/The 5 Best IDEs for WordPress Development (And Why)
/33 Popular WordPress User Interface Elements
/New Course: How to Hack Your Own App
/How to Install Yii on Windows or a Mac
/What Is a JavaScript Operator?
/How to Register and Use Laravel Service Providers
/
waly Good blog post. I absolutely love this…