jQuery

jQuery is a fast, small, and feature-rich JavaScript library. It simplifies HTML document traversal, event handling, animation, and AJAX interactions for rapid web development. It works seamlessly across various browsers, ensuring consistency and efficiency.

  1. Simplified Syntax: Allows for easier interaction with the DOM.

  2. Cross-Browser Compatibility: Handles browser inconsistencies.

  3. Rich Features: Provides powerful effects and animations.

  4. AJAX Support: Simplifies asynchronous HTTP requests.

  5. Community Support: Extensive documentation and plugins are available.


Adding jQuery via CDN

To include jQuery via CDN (Content Delivery Network), you can add the following <script> tag inside the <head> or just before the closing </body> tag of your HTML file:

<!DOCTYPE html>
<html>
<head>
    <title>Using jQuery from CDN</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <h1>Hello, jQuery!</h1>
    <script>
        $(document).ready(function(){
            $("h1").css("color", "blue");
        });
    </script>
</body>
</html>

Adding jQuery Locally

To add jQuery locally, follow these steps:

  1. Download jQuery: Visit the jQuery download pagearrow-up-right and download the compressed, production jQuery library.

  2. Include jQuery in Your Project: Place the downloaded jQuery file in your project's directory (e.g., js/jquery.min.js).

  3. Link the jQuery File in Your HTML: Add a <script> tag that points to the local jQuery file, similar to the example below:

Note: Using a CDN is generally recommended because it can result in faster loading times and better caching.


Sure! Here is the jQuery code only, with explanations in the notes:


1. Introduction to jQuery

Basic jQuery Syntax

Note: When the document is ready, clicking on a paragraph will hide it.


2. Selectors

Basic Selectors

Note: Changes the color of all paragraphs to blue.

Attribute Selectors

Note: Changes the color of links that open in a new tab to red.

Advanced Selectors

Note: Changes the color of the first paragraph to green.


3. Manipulating the DOM

Changing HTML Content

Note: Changes the HTML content of all paragraphs to "New content!".

Changing CSS Properties

Note: Changes the color of all paragraphs to blue.

Adding and Removing Elements

Note: Appends a new paragraph to the body of the document.

Traversing the DOM

Note: Changes the background color of the parent element of all paragraphs to yellow.


4. Handling Events

Basic Event Handling

Note: Hides the paragraph that is clicked on.

Event Delegation

Note: Hides any paragraph that is clicked on, even if it is added dynamically.

Custom Events

Note: Changes the color of paragraphs to blue when the custom event "custom" is triggered.


5. Animations and Effects

Show, Hide, Fade, Slide

Show and Hide:

Note: Shows or hides paragraphs when respective buttons are clicked.

Fade In and Fade Out:

Note: Fades in or out paragraphs when respective buttons are clicked.

Slide Up and Slide Down:

Note: Slides up or down paragraphs when respective buttons are clicked.

Animate

Note: Animates the div by moving it to the left, changing its opacity, and toggling its height.

Stop

Note: Stops the current animation of the div.


6. Working with AJAX

Using jQuery's AJAX Methods

Using $.get():

Note: Loads data from a server and places the returned data in the element with the id "result".

Handling JSON Data

Note: Loads JSON data from a server and places a property value in the element with the id "result".


7. Advanced Topics

Chaining

Note: Changes the color of paragraphs to red, then slides them up and down.

Callbacks

Note: Hides paragraphs and alerts a message when the animation is complete.

jQuery.noConflict()

Note: Releases the $ shorthand for jQuery and uses $j instead.

Filters and Custom Selectors

Note: Changes the background color of odd paragraphs to yellow.


Summary

jQuery is a versatile and user-friendly library that significantly simplifies JavaScript programming for web developers. Its cross-browser compatibility, powerful selectors, event handling, effects, and AJAX integration make it an essential tool for creating dynamic and interactive web pages. By understanding and practicing its features, developers can create efficient and visually appealing applications.

Last updated