(Translated by https://www.hiragana.jp/)
jQuery Tutorial | Learn jQuery With Examples - GeeksforGeeks
Open In App

jQuery Tutorial

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaScript tasks, making your code more readable and maintainable. By learning jQuery, you can significantly enhance the interactivity and responsiveness of your web pages. In this jQuery tutorial, you will learn all the basic to advanced concepts of jQuery such as DOM manipulation and event handling, AJAX, animations, plugins, etc.

What is jQuery?

jQuery is a lightweight, “write less, do more” JavaScript library that simplifies web development. It provides an easy-to-use API for common tasks, making it much easier to work with JavaScript on your website. It streamlines web development by providing a concise syntax and powerful utilities for creating interactive and dynamic web pages.

Features of jQuery

Here are some key points about jQuery:

  • DOM Manipulation: jQuery simplifies HTML DOM tree traversal and manipulation. You can easily select and modify elements on your web page.
  • Event Handling: Handling user interactions (such as clicks or mouse events) becomes straightforward with jQuery.
  • CSS Animations: You can create smooth animations and effects using jQuery.
  • AJAX (Asynchronous JavaScript and XML): jQuery simplifies making asynchronous requests to the server and handling responses.
  • Cross-Browser Compatibility: jQuery abstracts browser-specific inconsistencies, ensuring your code works consistently across different browsers.
  • Community Support: A large community of developers actively contributes to jQuery, ensuring continuous updates and improvements.
  • Plugins: jQuery has a wide range of plugins available for various tasks.
jQuery-Tutorial

Getting Started with jQuery

1. Download the jQuery Liberary

You can download the jQuery library from the official website jquery.com and include it in your project by linking to it using a <script> tag, and host it on your server or local filesystem.

2. Include the jQuery CDN in Code

Using jQuery Library Content Delivery Network (CDN) in your HTML project.

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

Once the library is included, you can write jQuery code within <script> tags. jQuery code typically follows a specific syntax:

<script>  $(document).ready(function() {    // Your jQuery code here  });</script>

The $(document).ready(function() { ... }) function ensures your code executes only after the document is fully loaded, preventing errors.

jQuery Basic Example

In this example, we are using hover() and css() methods to change the style of heading content on mouse move over.

html
<!DOCTYPE html>
<html>

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

    <script>
        $(document).ready(function () {
            $("h1").hover(
                function () {
                    $(this).css(
                        "color",
                        "green"
                    );
                },
                function () {
                    $(this).css(
                        "color",
                        "aliceblue"
                    );
                }
            );
        });
    </script>
</head>

<body>
    <h1>GeeksforGeeks</h1>
</body>

</html>

Output:

jQuery Basic Example

Steps to Learn jQuery – Complete jQuery Learning Path

jQuery Basics

jQuery Basics covers the fundamental concepts of the jQuery library, including its purpose, installation, and basic syntax. It introduces how to include jQuery in a web page and use its core functions to simplify JavaScript operations. Understanding the basics provides a foundation for using jQuery effectively to manipulate HTML, handle events, and create interactive web experiences.

jQuery Selectors

jQuery Selectors are used to select and manipulate HTML elements based on their attributes, classes, IDs, or other criteria. jQuery offers a powerful set of selectors that allow developers to target elements efficiently. Common selectors include ID selectors (#id), class selectors (.class), and attribute selectors ([attribute=value]).

For a comprehensive list of Selectors, please refer to the jQuery Selectors Complete Reference article.

jQuery Events

jQuery Events involve handling user interactions and other occurrences on a web page. jQuery provides methods to attach event handlers to elements, such as clicks, keypresses, and form submissions. Key functions include .on(), .off(), and .trigger(). Understanding how to work with events is essential for creating responsive and interactive web applications.

For a comprehensive list of Events, please refer to the jQuery Events Complete Reference article.

jQuery Effects

jQuery Effects enable the creation of animations and visual effects on web pages. jQuery includes built-in methods for showing, hiding, fading, sliding, and animating elements. Functions like .fadeIn(), .slideUp(), and .animate() help in creating dynamic and engaging user interfaces. Effects can enhance the user experience by providing visual feedback and transitions.

For a comprehensive list of Effects, please refer to the jQuery Effects Complete Reference article.

jQuery HTML/CSS

jQuery HTML/CSS focuses on manipulating the HTML content and CSS styles of elements using jQuery. This includes methods for changing element attributes, updating text or HTML content, and applying or modifying CSS styles. Functions such as .html(), .css(), and .addClass() allow developers to dynamically adjust the appearance and content of web elements.

For a comprehensive list of HTML/CSS, please refer to the jQuery HTML/CSS Complete Reference article.

jQuery Traversing

jQuery Traversing involves navigating the DOM tree to select and manipulate elements relative to each other. jQuery provides methods for moving up, down, and across the DOM hierarchy, such as .parent(), .children(), .siblings(), and .find(). Effective traversing helps in targeting specific elements based on their relationships within the document structure.

For a comprehensive list of Traversing, please refer to the jQuery Traversing Complete Reference article.

jQuery Ajax

jQuery Ajax facilitates asynchronous communication between the client and server, allowing web pages to update content dynamically without a full page reload. jQuery’s .ajax(), .get(), and .post() methods simplify making HTTP requests and handling responses. Understanding Ajax is essential for creating modern, responsive web applications that interact with server-side resources.

For a comprehensive list of Ajax, please refer to the jQuery Ajax Complete Reference article.

jQuery Properties

jQuery Properties cover methods for retrieving and setting properties of HTML elements. This includes getting or setting values, attributes, and other properties like dimensions and scroll positions. Functions such as .val(), .attr(), and .prop() enable precise control over element properties and are crucial for form handling and dynamic content updates.

jQuery Plugins

jQuery Plugins are extensions that add functionality to the core jQuery library. Plugins provide reusable components and features that can be easily integrated into web projects. Popular plugins include sliders, date pickers, and form validation tools. Understanding how to use and create plugins can significantly enhance the capabilities of jQuery and streamline development processes.

For a comprehensive list of Plugins, please refer to the jQuery Plugins Complete Reference article.

jQuery Projects

jQuery projects leverage the library’s features to create interactive and dynamic web applications. They often include functionalities like form validation, animations, and AJAX requests. By utilizing jQuery’s simple syntax and cross-browser compatibility, developers can efficiently build responsive and user-friendly interfaces, enhancing the overall user experience and streamlining development processes.

jQuery Interview Questions and Answers (2024)

Advantages of jQuery

  • Wide range of plug-ins that allows developers to create plug-ins on top of the JavaScript library.
  • Large development community.
  • It is a lot easier to use compared to standard javascript and other javascript libraries.
  • It lets users develop Ajax templates with ease. Ajax enables a sleeker interface where actions can be performed on pages without requiring the entire page to be reloaded.
  • Being Light weight and a powerful chaining capabilities makes it more strong.

jQuery Cheat Sheet

The jQuery Cheat Sheet provides a quick reference guide for developers, summarizing common jQuery methods, selectors, events, and syntax, making it easier to write and understand jQuery code efficiently.

jQuery Tutorial – FAQs

Why should I use jQuery?

jQuery simplifies JavaScript coding by providing easy-to-use methods for common tasks, such as manipulating the DOM, handling events, and making AJAX requests. It enhances productivity and ensures compatibility across different web browsers.

How do I include jQuery in my project?

You can include jQuery in your project by adding a <script> tag in your HTML file. You can either use a Content Delivery Network (CDN) or download and host the jQuery file locally. Example for CDN: <script src=”https://code.jquery.com/jquery-3.6.0.min.js”></script>.

What are jQuery selectors?

jQuery selectors are used to find and manipulate HTML elements on a web page. They follow a similar syntax to CSS selectors, allowing you to select elements by tag, class, id, attribute, and more.

How can I handle events with jQuery?

jQuery simplifies event handling with methods like .on(), .click(), .hover(), and .submit(). You can attach event handlers to elements and define the actions that should occur when an event is triggered.

How can I traverse the DOM with jQuery?

jQuery offers various methods to traverse the DOM, such as .parent(), .children(), .siblings(), and .find(). These methods help you navigate and manipulate the hierarchy of HTML elements.

What are jQuery plugins and how do I use them?

jQuery plugins are reusable code extensions that provide additional functionality. To use a plugin, you include the plugin’s JavaScript file and initialize it on your jQuery objects using the plugin’s specific method.



Similar Reads

jQuery Cheat Sheet – A Basic Guide to jQuery
What is jQuery?jQuery is an open-source, feature-rich JavaScript library, designed to simplify the HTML document traversal and manipulation, event handling, animation, and Ajax with an easy-to-use API that supports the multiple browsers. It makes the easy interaction between the HTML & CSS document, Document Object Model (DOM), and JavaScript.
15+ min read
jQuery jQuery.fx.interval Property with example
The jQuery.fx.interval property in jQuery is used to modify the number of frames per second at which animations will run and to change the animation firing rate in milliseconds. Its default value is 13ms. Syntax: jQuery.fx.interval = milliseconds;Parameters: This method accepts single parameter milliseconds which is mandatory. It is used to specify
2 min read
jQuery jQuery.fx.off Property
The jQuery.fx.off property in jQuery is used to globally disable/enable all animations. Its default value is false which is used to allow animation to run normally. Syntax: jQuery.fx.off = true|false;Parameter: This event accepts two parameters as mentioned above and described below: true: It is used to specify that the animations should be disable
2 min read
jQuery jQuery.support Property
The jQuery.support property in jQuery contains a collection of properties that are used to represent the different browser features or bugs. Syntax: jQuery.support.propvalueParameters: This property contains a single parameter propvalue: which is required. It is used to specify the function to test for. The tests included are: ajaxboxModelchangeBub
1 min read
jQuery jquery Property
The jquery property is used to return the jQuery version number. Syntax $().jqueryExample: Return the version number. C/C++ Code <!DOCTYPE html> <html> <head> <style> h1 { color: green; } </style> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script
1 min read
How to append a jQuery object to all paragraphs using jQuery ?
In this article, we will see how to append a jQuery object to all paragraphs using jQuery. Append means we add something to an existing element. The object is used to define a container for an external resource like a page, a picture, a media player, or a plug-in application. Used Methods: ready() Method: This method is used to specify a function t
2 min read
How to insert a jQuery object after all paragraphs using jQuery ?
The task is to insert a jQuery object after all the paragraphs using jQuery. Approach: Create DOM element or jQuery object using <span> tag.Create a paragraph using <p> tag.Create a button using button tag after clicking on which the object will be inserted after paragraph.Write a script that will call a function jQuery.after() which is
1 min read
Difference between jquery.size() and jquery.length
JQuery.size() method gives us the number of elements present. For Example, if we are calling the size() method for "p" tag, then it will return the number of "p" tags present on our page. Syntax: $(selector).size() Return value: It returns the number of “selector” present. Example: C/C++ Code <!DOCTYPE html> <html lang="en">
2 min read
How to remove close button from jQuery UI dialog using jQuery ?
In this article, we will learn how to remove the close button on the jQuery UI dialog using JavaScript, This can be achieved using the hide() method. jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. A dialog box is a temporary window. An application creates a dial
2 min read
How to make a JSON call using jQuery ?
Use the getJSON() function in jQuery to load JSON data. The getJSON() function uses a GET HTTP request to retrieve JSON-encoded data from the server. In this article, we will learn about the jQuery getJSON() function and its implementation through examples. Syntax: $(selector).getJSON(url, data, success(data, status, xhr)) Parameters: This method a
3 min read
JQuery Multiple ID selectors
Given an HTML document and the task is to select the elements with different ID's at the same time using JQuery. Approach: Select the ID's of different element and then use each() method to apply the CSS property on all selected ID's element.Then use css() method to set the background color to pink to all selected elements.Display the text which in
2 min read
How to create a Fieldcontain mini sized using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Fieldcontain mini sized using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=”stylesheet” href=”http://code.jquery.com/mobile/1.4.5/jq
1 min read
jQuery :input Selector
The jQuery :input selector in jQuery is used to select input elements. This selector is also used with button element. Syntax: $(":input")Note: jQuery :input selector not only selects input elements but also Buttons, Dropdowns, and Textarea elements. Example 1: This example use :input selector to count all input elements. C/C++ Code <!DOCTYPE ht
2 min read
jQuery Mobile Collapsibleset refresh() Method
jQuery Mobile is a web-based technology that can be used to make responsive content for websites that can be accessed on all types of smartphones, tablets, and desktops and it is also used by many users. In this article, we will be using the jQuery Mobile Collapsibleset refresh() method to update the collapsibleset widget. This method is used to up
2 min read
jQuery Mobile Page closeBtn Option
jQuery Mobile is a web-based technology designed to make responsive websites and apps that are accessible on all smartphone, tablet, and desktop devices. PageWidget in jQuery Mobile is responsible for maintaining a single item in jQuery Mobile's framework. In this article, we are going to learn the jQuery Mobile Page closeBtn option. The closeBtn o
2 min read
jQuery UI Sortable widget() Method
jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add widgets easily. In this article, we will be using
2 min read
Explain source maps in jQuery
Source maps are a powerful tool for debugging and troubleshooting code, particularly when working with large or complex codebases. They allow developers to easily understand the relationships between the original, unminified code and the compiled or minimized code that is actually running in the browser. In other words, source maps provide a way to
6 min read
jQuery prepend() Method
The prepend() method is an inbuilt method in jQuery that is used to insert specified content at the beginning of the selected element. Syntax: $(selector).prepend(content, function)Parameters: This method accepts two parameters as mentioned above and described below: content: It is a required parameter that is used to specify the content that needs
2 min read
How to make a Mini sized vertical controlgroups using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making a Mini sized vertical controlgroups using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" href="http://code.jquery.com/
2 min read
How to create five-column grid in jQuery Mobile ?
Grids in jQuery Mobile are elements that have 100% width and are completely invisible having no borders, backgrounds, padding, or margins. Inside the grid container, the elements are assigned the classes with the names ui-block-a/b/c/d/e. This makes each "block" element appear in the form of a grid by floating next to each other. Creating a five-co
1 min read
How to Create a Flashing Text Effect using jQuery ?
In this article, we are going to create a flashing text using jQuery. It is basically, a text which is visible then it goes invisible, and again it comes back to its original visibility and this process is continued repeatedly, then it is called flashing text. With jQuery, it is very simple to create blinking or flashing text. There are some built-
2 min read
jQuery offset() Method
The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element.Syntax: $(selector).offset()Parameters: Parameter does not required.$(selector).offset({top:value, left:value})Parameters: The parameter is required when set the offset.$(selector).offset( function(index, offset) )Parame
2 min read
jQuery Mobile Pagecontainer Widget Complete Reference
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. The Pagecontainer Widget is used to manage the collection of pages in jQuery Mobile. jQuery Mobile Pagecontainer Widget Options jQuery Mobile Pagecontainer classes OptionjQuery Mobile Pagecontainer defaults Optionj
1 min read
How to find the parent class name with known class in jQuery ?
Given an HTML document and the task is to get the parent class of an element with the help of given class name. Approach 1: The on() method is used to select the event handler for selected elements. It means when the user clicks the button then the function call. The closest() method is used to return the first ancestor of the selected elements. Ch
3 min read
EasyUI jQuery messager widget
EasyUI is an HTML5 framework for using user interface components based on jQuery, React, Angular and Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers. In this article, we will learn how to design a messager using jQuery EasyUI. messager widget provide different styles of
2 min read
How to remove all attributes of an HTML element using jQuery ?
In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, function() { this.ownerElement.removeAttributeNode(
1 min read
How to add a class on click of anchor tag using jQuery ?
In this article, we will see how to add a class on click of an anchor tag using jQuery. To add a class on click of the anchor tag, we use the addClass() method. The addClass() method is used to add more properties to each selected element. It can also be used to change the property of the selected element. We can also use toggleClass for adding and
2 min read
jQuery :nth-of-type() Selector
The :nth-of-type() is an inbuilt selector in jQuery, used to select all the nth-child elements of the specified parent. Syntax: parent_name : nth-of-type(n|even|odd|algebraic equation)Parameters: It takes a parameter n | even | odd | algebraic equation. ValueDescriptionnSelect the child present at nth index (starting from 1). n must be an integer.e
3 min read
jQuery UI Tooltip widget() Method
jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. jQuery UI tooltip widget helps us to add new themes and allows for customization. In this article, we will see how to use widget method in jQuery UI tooltips. The widget method is use
1 min read
jQuery Mobile panel classes.animate Option
jQuery Mobile is a web-based technology that can be used to make responsive content for websites that can be accessed on all types of smartphones, tablets, and desktops. In this article, we will learn how to use the jQuery Mobile panel classes.animate Option. Using this option, classes can be added to the panel, page contents wrapper, and fixed too
2 min read