jQuery Introduction

jQuery Introduction
❮ Previous Next ❯
The purpose of jQuery is to make it much easier to use JavaScript on your website.

What You Should Already Know
Before you start studying jQuery, you should have a basic knowledge of:

HTML
CSS
JavaScript
If you want to study these subjects first, find the tutorials on our Home page.

What is jQuery?
jQuery is a lightweight, “write less, do more”, JavaScript library.

The purpose of jQuery is to make it much easier to use JavaScript on your website.

jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

The jQuery library contains the following features:

HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
Tip: In addition, jQuery has plugins for almost any task out there.

Why jQuery?
There are lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.

Many of the biggest companies on the Web use jQuery, such as:

Google
Microsoft
IBM
Netflix
Will jQuery work in all browsers?

The jQuery team knows all about cross-browser issues, and they have written this knowledge into the jQuery library. jQuery will run exactly the same in all major browsers, including Internet Explorer 6!

❮ Previous Next ❯

jQuery Tutorial

Toggle navigation
TUTORIAL HOME
jQuery Tutorial
❮ Home Next ❯
jQuery is a JavaScript Library.

jQuery greatly simplifies JavaScript programming.

jQuery is easy to learn.

“” Examples in Each Chapter
With our online editor, you can edit the code, and click on a button to view the result.

Example
$(document).ready(function(){
    $(“p”).click(function(){
        $(this).hide();
    });
});
»
Click on the “” button to see how it works.

jQuery Examples
Learn by examples! At Omegas you will find a lot of jQuery examples to edit and test yourself.

jQuery Examples
jQuery Quiz Test
Test your jQuery skills at Omegas!

jQuery Quiz
jQuery References
At Omegas you will find a complete reference of all jQuery objects and methods.

jQuery Reference

❮ Home Next ❯

jQuery Get Started

Toggle navigation
TUTORIAL HOME
jQuery Get Started
❮ Previous Next ❯
Adding jQuery to Your Web Pages
There are several ways to start using jQuery on your web site. You can:

Download the jQuery library from jQuery.com
Include jQuery from a CDN, like Google
Downloading jQuery
There are two versions of jQuery available for downloading:

Production version – this is for your live website because it has been minified and compressed
Development version – this is for testing and development (uncompressed and readable code)
Both versions can be downloaded from jQuery.com.

The jQuery library is a single JavaScript file, and you reference it with the HTML tag (notice that the tag should be inside the section):

</script>

Tip: Place the downloaded file in the same directory as the pages where you wish to use it.
Do you wonder why we do not have type=”text/javascript” inside the tag?

This is not required in HTML5. JavaScript is the default scripting language in HTML5 and in all modern browsers!

jQuery CDN
If you don’t want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).

Both Google and Microsoft host jQuery.

To use jQuery from Google or Microsoft, use one of the following:

Google CDN:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js“>

»
Microsoft CDN:

<script src="https://ajax.htmlnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js“>

»
One big advantage of using the hosted jQuery from Google or Microsoft:

Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time. Also, 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, which also leads to faster loading time.

❮ Previous Next ❯

jQuery Syntax

TUTORIAL HOME
jQuery Syntax
❮ Previous Next ❯
With jQuery you select (query) HTML elements and perform “actions” on them.

jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).

Basic syntax is: $(selector).action()

A $ sign to define/access jQuery
A (selector) to “query (or find)” HTML elements
A jQuery action() to be performed on the element(s)
Examples:

$(this).hide() – hides the current element.

$(“p”).hide() – hides all

elements.

$(“.test”).hide() – hides all elements with class=”test”.

$(“#test”).hide() – hides the element with id=”test”.

Are you familiar with CSS selectors?

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next chapter of this tutorial.

The Document Ready Event
You might have noticed that all jQuery methods in our examples, are inside a document ready event:

$(document).ready(function(){

   // jQuery methods go here…

});
This is to prevent any jQuery code from running before the document is finished loading (is ready).

It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.

Here are some examples of actions that can fail if methods are run before the document is fully loaded:

Trying to hide an element that is not created yet
Trying to get the size of an image that is not loaded yet
Tip: The jQuery team has also created an even shorter method for the document ready event:

$(function(){

   // jQuery methods go here…

});
Use the syntax you prefer. We think that the document ready event is easier to understand when reading the code.

❮ Previous Next ❯

jQuery Selectors

TUTORIAL HOME
jQuery Selectors
❮ Previous Next ❯
jQuery selectors are one of the most important parts of the jQuery library.

jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s).

jQuery selectors are used to “find” (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It’s based on the existing CSS Selectors, and in addition, it has some own custom selectors.

All selectors in jQuery start with the dollar sign and parentheses: $().

The element Selector
The jQuery element selector selects elements based on the element name.

You can select all

elements on a page like this:

$(“p”)
Example

When a user clicks on a button, all

elements will be hidden:

Example
$(document).ready(function(){
    $(“button”).click(function(){
        $(“p”).hide();
    });
});
»
The #id Selector
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

To find an element with a specific id, write a hash character, followed by the id of the HTML element:

$(“#test”)
Example

When a user clicks on a button, the element with id=”test” will be hidden:

Example
$(document).ready(function(){
    $(“button”).click(function(){
        $(“#test”).hide();
    });
});
»
The .class Selector
The jQuery class selector finds elements with a specific class.

To find elements with a specific class, write a period character, followed by the name of the class:

$(“.test”)
Example

When a user clicks on a button, the elements with class=”test” will be hidden:

Example
$(document).ready(function(){
    $(“button”).click(function(){
        $(“.test”).hide();
    });
});
»
More Examples of jQuery Selectors
Syntax Description Example
$(“*”) Selects all elements Try it
$(this) Selects the current HTML element Try it
$(“p.intro”) Selects all

elements with class=”intro” Try it
$(“p:first”) Selects the first

element Try it
$(“ul li:first”) Selects the first

  • element of the first
  • jQuery Effects – Hide and Show

    TUTORIAL HOME
    jQuery Effects – Hide and Show
    ❮ Previous Next ❯
    Hide, Show, Toggle, Slide, Fade, and Animate. WOW!

    Click to show/hide panel

    Because time is valuable, we deliver quick and easy learning.

    At Omegas, you can study everything you need to learn, in an accessible and handy format.

    Examples
    jQuery hide()
    Demonstrates a simple jQuery hide() method.

    jQuery hide()
    Another hide() demonstration. How to hide parts of text.

    jQuery hide() and show()
    With jQuery, you can hide and show HTML elements with the hide() and show() methods:

    Example
    $(“#hide”).click(function(){
        $(“p”).hide();
    });

    $(“#show”).click(function(){
        $(“p”).show();
    });
    »
    Syntax:

    $(selector).hide(speed,callback);

    $(selector).show(speed,callback);
    The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the hide() or show() method completes (you will learn more about callback functions in a later chapter).

    The following example demonstrates the speed parameter with hide():

    Example
    $(“button”).click(function(){
        $(“p”).hide(1000);
    });
    »
    jQuery toggle()
    With jQuery, you can toggle between the hide() and show() methods with the toggle() method.

    Shown elements are hidden and hidden elements are shown:

    Example
    $(“button”).click(function(){
        $(“p”).toggle();
    });
    »
    Syntax:

    $(selector).toggle(speed,callback);
    The optional speed parameter can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after toggle() completes.

    Test Yourself with Exercises!
    Exercise 1 »  Exercise 2 »  Exercise 3 »  Exercise 4 »

    jQuery Effects Reference
    For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.

    ❮ Previous Next ❯

    jQuery Effects – Sliding

    jQuery Effects – Sliding
    ❮ Previous Next ❯
    The jQuery slide methods slide elements up and down.

    Click to slide down/up the panel

    Because time is valuable, we deliver quick and easy learning.

    At Omegas, you can study everything you need to learn, in an accessible and handy format.

    Examples
    jQuery slideDown()
    Demonstrates the jQuery slideDown() method.

    jQuery slideUp()
    Demonstrates the jQuery slideUp() method.

    jQuery slideToggle()
    Demonstrates the jQuery SlideToggle() method.

    jQuery Sliding Methods
    With jQuery you can create a sliding effect on elements.

    jQuery has the following slide methods:

    slideDown()
    slideUp()
    slideToggle()
    jQuery slideDown() Method
    The jQuery slideDown() method is used to slide down an element.

    Syntax:

    $(selector).slideDown(speed,callback);
    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the sliding completes.

    The following example demonstrates the slideDown() method:

    Example
    $(“#flip”).click(function(){
        $(“#panel”).slideDown();
    });
    »
    jQuery slideUp() Method
    The jQuery slideUp() method is used to slide up an element.

    Syntax:

    $(selector).slideUp(speed,callback);
    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the sliding completes.

    The following example demonstrates the slideUp() method:

    Example
    $(“#flip”).click(function(){
        $(“#panel”).slideUp();
    });
    »
    jQuery slideToggle() Method
    The jQuery slideToggle() method toggles between the slideDown() and slideUp() methods.

    If the elements have been slid down, slideToggle() will slide them up.

    If the elements have been slid up, slideToggle() will slide them down.

    $(selector).slideToggle(speed,callback);
    The optional speed parameter can take the following values: “slow”, “fast”, milliseconds.

    The optional callback parameter is a function to be executed after the sliding completes.

    The following example demonstrates the slideToggle() method:

    Example
    $(“#flip”).click(function(){
        $(“#panel”).slideToggle();
    });
    »
    Test Yourself with Exercises!
    Exercise 1 »  Exercise 2 »  Exercise 3 »  Exercise 4 »

    jQuery Effects Reference
    For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.

    ❮ Previous Next ❯

    jQuery Effects – Fading

    jQuery Effects – Fading
    ❮ Previous Next ❯
    With jQuery you can fade elements in and out of visibility.

    Click to fade in/out panel

    Because time is valuable, we deliver quick and easy learning.

    At Omegas, you can study everything you need to learn, in an accessible and handy format.

    Examples
    jQuery fadeIn()
    Demonstrates the jQuery fadeIn() method.

    jQuery fadeOut()
    Demonstrates the jQuery fadeOut() method.

    jQuery fadeToggle()
    Demonstrates the jQuery fadeToggle() method.

    jQuery fadeTo()
    Demonstrates the jQuery fadeTo() method.

    jQuery Fading Methods
    With jQuery you can fade an element in and out of visibility.

    jQuery has the following fade methods:

    fadeIn()
    fadeOut()
    fadeToggle()
    fadeTo()
    jQuery fadeIn() Method
    The jQuery fadeIn() method is used to fade in a hidden element.

    Syntax:

    $(selector).fadeIn(speed,callback);
    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the fading completes.

    The following example demonstrates the fadeIn() method with different parameters:

    Example
    $(“button”).click(function(){
        $(“#div1”).fadeIn();
        $(“#div2”).fadeIn(“slow”);
        $(“#div3”).fadeIn(3000);
    });
    »
    jQuery fadeOut() Method
    The jQuery fadeOut() method is used to fade out a visible element.

    Syntax:

    $(selector).fadeOut(speed,callback);
    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the fading completes.

    The following example demonstrates the fadeOut() method with different parameters:

    Example
    $(“button”).click(function(){
        $(“#div1”).fadeOut();
        $(“#div2”).fadeOut(“slow”);
        $(“#div3”).fadeOut(3000);
    });
    »
    jQuery fadeToggle() Method
    The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.

    If the elements are faded out, fadeToggle() will fade them in.

    If the elements are faded in, fadeToggle() will fade them out.

    Syntax:

    $(selector).fadeToggle(speed,callback);
    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the fading completes.

    The following example demonstrates the fadeToggle() method with different parameters:

    Example
    $(“button”).click(function(){
        $(“#div1”).fadeToggle();
        $(“#div2”).fadeToggle(“slow”);
        $(“#div3”).fadeToggle(3000);
    });
    »
    jQuery fadeTo() Method
    The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).

    Syntax:

    $(selector).fadeTo(speed,opacity,callback);
    The required speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The required opacity parameter in the fadeTo() method specifies fading to a given opacity (value between 0 and 1).

    The optional callback parameter is a function to be executed after the function completes.

    The following example demonstrates the fadeTo() method with different parameters:

    Example
    $(“button”).click(function(){
        $(“#div1”).fadeTo(“slow”, 0.15);
        $(“#div2”).fadeTo(“slow”, 0.4);
        $(“#div3”).fadeTo(“slow”, 0.7);
    });
    »
    Test Yourself with Exercises!
    Exercise 1 »  Exercise 2 »  Exercise 3 »  Exercise 4 »  Exercise 5 »

    jQuery Effects Reference
    For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.

    ❮ Previous Next ❯

    jQuery Effects – Animation

    Toggle navigation
    TUTORIAL HOME
    jQuery Effects – Animation
    ❮ Previous Next ❯
    The jQuery animate() method lets you create custom animations.

    Start Animation

    jQuery
    jQuery Animations – The animate() Method
    The jQuery animate() method is used to create custom animations.

    Syntax:

    $(selector).animate({params},speed,callback);
    The required params parameter defines the CSS properties to be animated.

    The optional speed parameter specifies the duration of the effect. It can take the following values: “slow”, “fast”, or milliseconds.

    The optional callback parameter is a function to be executed after the animation completes.

    The following example demonstrates a simple use of the animate() method; it moves a

    element to the right, until it has reached a left property of 250px:

    Example
    $(“button”).click(function(){
        $(“div”).animate({left: ‘250px’});
    });
    »
    By default, all HTML elements have a static position, and cannot be moved.
    To manipulate the position, remember to first set the CSS position property of the element to relative, fixed, or absolute!

    jQuery animate() – Manipulate Multiple Properties
    Notice that multiple properties can be animated at the same time:

    Example
    $(“button”).click(function(){
        $(“div”).animate({
            left: ‘250px’,
            opacity: ‘0.5’,
            height: ‘150px’,
            width: ‘150px’
        });
    });
    »
    Is it possible to manipulate ALL CSS properties with the animate() method?

    Yes, almost! However, there is one important thing to remember: all property names must be camel-cased when used with the animate() method: You will need to write paddingLeft instead of padding-left, marginRight instead of margin-right, and so on.

    Also, color animation is not included in the core jQuery library.
    If you want to animate color, you need to download the Color Animations plugin from jQuery.com.

    jQuery animate() – Using Relative Values
    It is also possible to define relative values (the value is then relative to the element’s current value). This is done by putting += or -= in front of the value:

    Example
    $(“button”).click(function(){
        $(“div”).animate({
            left: ‘250px’,
            height: ‘+=150px’,
            width: ‘+=150px’
        });
    });
    »
    jQuery animate() – Using Pre-defined Values
    You can even specify a property’s animation value as “show”, “hide”, or “toggle”:

    Example
    $(“button”).click(function(){
        $(“div”).animate({
            height: ‘toggle’
        });
    });
    »
    jQuery animate() – Uses Queue Functionality
    By default, jQuery comes with queue functionality for animations.

    This means that if you write multiple animate() calls after each other, jQuery creates an “internal” queue with these method calls. Then it runs the animate calls ONE by ONE.

    So, if you want to perform different animations after each other, we take advantage of the queue functionality:

    Example 1
    $(“button”).click(function(){
        var div = $(“div”);
        div.animate({height: ‘300px’, opacity: ‘0.4’}, “slow”);
        div.animate({width: ‘300px’, opacity: ‘0.8’}, “slow”);
        div.animate({height: ‘100px’, opacity: ‘0.4’}, “slow”);
        div.animate({width: ‘100px’, opacity: ‘0.8’}, “slow”);
    });
    »
    The example below first moves the

    element to the right, and then increases the font size of the text:

    Example 2
    $(“button”).click(function(){
        var div = $(“div”);
        div.animate({left: ‘100px’}, “slow”);
        div.animate({fontSize: ‘3em’}, “slow”);
    });
    »
    Test Yourself with Exercises!
    Exercise 1 »  Exercise 2 »  Exercise 3 »  Exercise 4 »

    jQuery Effects Reference
    For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.

    ❮ Previous Next ❯

    jQuery Stop Animations

    Toggle navigation
    TUTORIAL HOME
    jQuery Stop Animations
    ❮ Previous Next ❯
    The jQuery stop() method is used to stop animations or effects before it is finished.

    Start slidingStop sliding

    Click to slide down/up the panel

    Because time is valuable, we deliver quick and easy learning.

    At Omegas, you can study everything you need to learn, in an accessible and handy format.

    Examples
    jQuery stop() sliding
    Demonstrates the jQuery stop() method.

    jQuery stop() animation (with parameters)
    Demonstrates the jQuery stop() method.

    jQuery stop() Method
    The jQuery stop() method is used to stop an animation or effect before it is finished.

    The stop() method works for all jQuery effect functions, including sliding, fading and custom animations.

    Syntax:

    $(selector).stop(stopAll,goToEnd);
    The optional stopAll parameter specifies whether also the animation queue should be cleared or not. Default is false, which means that only the active animation will be stopped, allowing any queued animations to be performed afterwards.

    The optional goToEnd parameter specifies whether or not to complete the current animation immediately. Default is false.

    So, by default, the stop() method kills the current animation being performed on the selected element.

    The following example demonstrates the stop() method, with no parameters:

    Example
    $(“#stop”).click(function(){
        $(“#panel”).stop();
    });
    »
    jQuery Effects Reference
    For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.

    ❮ Previous Next ❯