Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • dislike -4 23

    What Is jQuery?

    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.

    What Is jQuery?

    First released in 2006, jQuery set out to be a cross-platform JavaScript library that made it easier to write client-side solutions.

    The jQuery HomepageThe jQuery HomepageThe jQuery Homepage

    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.

    HTML Document Traversal

    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:

    An example of the DOM for a basic web pageAn example of the DOM for a basic web pageAn example of the DOM for a basic web page

    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.

    If you're trying to loop through all of the span elements, you can do that as well:

    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.

    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 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:

    • determine the height or width of the document, the window, or any given element
    • grab the values from any given element (assuming it offers this ability)
    • toggle class names
    • and much more

    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.

    Event Handling

    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.

    Animation

    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.

    AJAX

    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.

    Extensibility With jQuery Plugins

    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.

    jQuery UI

    The jQuery UI HomepageThe jQuery UI HomepageThe jQuery UI Homepage

    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.

    jQuery Mobile

    As of 2021, jQuery Mobile is deprecated and no longer supported. You can read more about the status of jQuery Mobile on the jQuery blog.

    Do You Need jQuery Today?

    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.

    Reasons to Use jQuery in 2022

    1. Write Less Code

    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.

    Here is the vanilla JavaScript that you would have to use:

    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:

    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.

    2. The Project Already Depends On It

    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.

    3. The jQuery Plugin Ecosystem

    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.

    Additional Resources

    Conclusion

    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.

    martijn broeders

    founder/ strategic creative bij shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    telefoon: 434 210 0245

Per - categorie

    Op - datum