Home [Javascript] Introduction to jQuery
Post
Cancel

[Javascript] Introduction to jQuery

Intro

What is jQuery?

jQuery is a fast, small, and feature-rich JavaScript library. It simplifies HTML document traversal and manipulation, event handling, animation, and Ajax interactions. jQuery is designed to be easy to use and versatile, making it a popular choice for web developers to streamline coding processes and enhance user experience.

Syntax Overview

jQuery simplifies JavaScript programming by abstracting a lot of the complexities of DOM manipulation into a cleaner and more concise syntax. Here is a basic example of jQuery syntax:

1
2
3
$(document).ready(function(){
    // jQuery code goes here
});

In the above code snippet, $(document).ready() is a jQuery function that ensures the DOM is fully loaded before executing the code within the function. This helps prevent issues with elements not being available for manipulation.

Example Codes

  1. Selecting Elements: ```javascript // Select element by ID $(‘#myElementId’).css(‘color’, ‘red’);

// Select element by class $(‘.myClass’).hide();

// Select multiple elements $(‘p, div’).addClass(‘highlight’);

1
2
3
4
5
6
7
8
9
10
11
12
13
2. Event Handling:
```javascript
// Click event
$('#myButton').click(function(){
    alert('Button clicked!');
});

// Hover event
$('#myElement').hover(
    function(){ $(this).css('color', 'blue')},
    function(){ $(this).css('color', 'black')}
);
  1. Ajax Interaction:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    $.ajax({
     url: 'https://api.example.com/data',
     method: 'GET',
     success: function(response){
         console.log(response);
     },
     error: function(xhr, status, error){
         console.error(error);
     }
    });
    

Versions and Compatibility

jQuery has evolved over the years with new features and improvements. It is important to note that the latest stable version of jQuery at the time of writing is 3.6.0. Compatibility with different browsers is another crucial aspect to consider when using jQuery, as it aims to provide cross-browser support.

In conclusion, jQuery is a powerful tool for simplifying JavaScript programming and enhancing web development capabilities. By mastering jQuery syntax and leveraging its features, developers can create dynamic and interactive web applications with ease.

This post is licensed under CC BY 4.0 by the author.
Contents