jQuery is a fast, small, and feature-rich JavaScript library . Its purpose is to make using JavaScript on your website much simpler and easier. jQuery takes a lot of common tasks that to accomplish, require many lines of JavaScript code and wraps them into methods that you can call with a single line of code. It also simplifies more complex JavaScript processes, like AJAX calls and DOM manipulation, and is one of the most popular JavaScript libraries. The jQuery library contains the following features:
The jQuery library is a single JavaScript file, which is referenced with the HTML <script> tag (this should be inside the <head> tag's section). It can also be included from a CDN (Content Delivery Network), instead of downloading and hosting it yourself, such as Google's hosted version. Using the hosted (CDN) version from Google is advantageous because many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from a cache when they visit your site, enabling faster loading time. Additionally, most CDN's will make sure that once a user requests a file from it, it will be served from the server closest to them, enabling faster loading time. ( Programming in JavaScript IV: jQuery, HyperionDev 2021 )
It is generally recommended to use separate script files rather than having the script within your HTML file. This allows for efficient code layout and readability. To create a separate scripting file for your HTML as well as incorporating the jQuery library, place it inside the <head> tag's section. For example the image below shows this page's HTML code, with the CDN and separate scripting file placed in the <head> tag.
With jQuery you select (or query) HTML elements and perform "actions" on them . The jQuery basic syntax looks like this:
$(selector).action()
Where:
Some examples are:
Everything in jQuery happens thanks to functions ( Programming in JavaScript IV: jQuery, HyperionDev 2021 ). It is important to remember that functions should only operate once a document has completely loaded. This prevents any jQuery code from running before the document is finished loading (i.e. is 'ready'). We can do this by wrapping all our jQuery methods inside a document ready event. We can write this function declaration with either the standard jQuery definition (example 1 below), or with a shorter method that does the same thing (example 2 below). Note that as of jQuery 3.0, only the first syntax is recommended; the other syntaxes still work but are deprecated ( jQuery API Documentation ). Since this page is using a later version of jQuery, example 1 below displays the ready function as deprecated (strikethrough).
jQuery selectors are one of the most important parts of the jQuery library ( Programming in JavaScript IV: jQuery, HyperionDev 2021 ). They allow you to select and manipulate HTML elements. We use selectors to find HTML elements based on their name, ID, classes, types, attributes, values of attributes and more, giving us full control over how the webpage functions. jQuery selectors are based on the existing CSS Selectors, along with its own customised selectors. All selectors in jQuery start with the dollar sign and parentheses: $(). Some of the most common selectors are described below.
jQuery is tailor-made to respond to events in an HTML page. Events are all the user actions that a web page can respond to, for example moving or hovering the mouse over an element, selecting from a dropdown menu, clicking on an element or keyboard button. Events are represented by some action, and jQuery generates a response at the exact moment that the action occurs. Examples of common events used in jQuery are:
Mouse Events | Keyboard Events | Form Events |
---|---|---|
Click | Key press | Submit |
Double click | Key down | Change |
Mouse enter | Key up | Blur |
Effects can be described as actions which do something to HTML elements on your web page ( Programming in JavaScript IV: jQuery, HyperionDev 2021 ). jQuery makes it easy to add simple, standard animations and effects to your web page, as well as create more sophisticated custom effects . Examples of common effects used in jQuery can be found below:
Effect | Description |
---|---|
Hide | Hides the selected element |
Show | Reveals the selected element |
Fade | Fades the element in or out |
Animate | Animates some property of the element |
Stop | Stops any effect of an element |
Effect methods are implemented in the following order ( Programming in JavaScript IV: jQuery, HyperionDev 2021 ):
jQuery allows us to chain actions all into a single command, which allows for endless possibilities. However, commands should make sense when being executed in this order ( Programming in JavaScript IV: jQuery, HyperionDev 2021 ). Below is an example of a chained effect command that will select a button, change its colour, slide it down the page, and then back up again:
This example demonstrates how we can use jQuery to fade an element in or out, such as an image.
First, we assign the <img> element we want to animate to a class called "fadingImage":
Next, we assign the button elements we want to use as event listeners for this action to the classes "fadeOutButton" and "fadeInButton" respectively:
Finally, we create the functions that allow the button to "listen" for the click event and either slowly fade out or quickly fade in the image depending on which button is clicked (note the arguments for the fading speeds):
Try fading this image in and out by clicking the buttons. To see what's going on behind the scenes, right-click anywhere on the page and click inspect.
This example demonstrates how we can use jQuery to animate a paragraph, allowing users to move the parapgraph and change its background colours by pressing arrow keys.
First, we assign the <p> element we want to animate to a class called "movingParagraph":
Next, we create the function to animate the paragraph (i.e. to allow the user to move and change the colour of the <img> element with arrow keys). We do this with an 'on keydown' function that encases a switch statement (for the different key presses). Note that the code body in the switch statement's cases consists of chained effects to create the animations.
Try moving this paragraph with your keyboard's arrow keys. To see what's going on behind the scenes, right-click anywhere on the page and click inspect.
Use the arrow keys to move me around and change my colour.