Open In App

Getting Started with jQuery

jQuery is a fast lightweight, and feature-rich JavaScript library that simplifies many common tasks in web development. It was created by John Resign and first released in 2006. It makes it easier to manipulate HTML documents, handle events, create animations, and interact with DOM(Document Object Model).

Prerequisites

How to use jQuery in a Project?

To use JQuery in your project, we must include JQuery in our project. There are several ways to do this task. In this part, I am using the CDN (Content Delivery Network) to do this task. Include this CDN link in your html document in the head tag.

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

jQuery Basics

Syntax:

$(selector).action()

Parameters:

Example:

$("#test").hide() 

Selecting Elements:

In JQuery, selecting elements is similar to CSS. JQuery selection allows you to target and manipulate HTML elements on a web page. The basic syntax for selecting elements in JQuery is similar to CSS selectors.

Syntax:

$("element_name_id_class") 

Manipulating Elements:

In JQuery, manipulating elements involves selecting HTML elements and performing various actions like changing their content, modifiying attributes, applying styles, or handling events.

$(document).ready(function() {
// Change text content of a paragraph
$('p').text('Geeks For Geeks');
});
$(document).ready(function() {
// Change HTML content of a div
$('#myDiv').html('<strong>New HTML content</strong>');
});

Handling Events:

In JQuery, Handling events is very easy. It is also a crucial aspect of web development. Handling events allows you to respond to user interaction such as clicks, keypresses, or form submission. Here's a basic example of Handling Events in JQuery

$(document).ready(function() {
// Selecting a button element
$('button').click(function() {
$('p').text ("Geeks for Geeks)
});
});

Ajax Requests:

Ajax(Asynchronous JavaScript and XML) allows you to send and receive data from the server without reloading the entire page. JQuery simplifies this process with its AJAX methods. There are many methods in JQuery for handling AJAX. Here, I have give very simple example to load the data using AJAX request. you can read more about ajax() method.

Filename: data.txt

Geeks For Geeks
Geeks for Geeks
Geeks for Geeks

Steps to run the code

Running a jQuery project involves a few key steps. jQuery is a JavaScript library, so you'll need to create HTML, CSS, and JavaScript files to build your project.

Step 1: Set Up Your Project

Step 2: Include jQuery Library:

Step 3: Run Your Project

Advantages of using jQuery

These are the advantagse of using JQuery:

Example: This example shows the event o click by using jQuery.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Geeks for Geeks Project</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js">
      </script>
        
</head>
<body>

    <p>
        I am Technical Writer
    </p>

    <button id="btn">
        Click Me for changing Text
    </button>
  
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
              $('p').load('data.txt') ;
            });
          });
    </script>
</body>
</html>

Output:

videoNewVideo

Conclusion

In Conclusion, getting started with JQuery provides a powerful and efficent way to enhance a web development by simplifying common task such as DOM manipulation, event handling, and AJAX requests. It accessible for both beginners and experienced developers. JQuery allows you to achieve complex operations with minimal code, resulting in faster development and improvied cross-browser compatiblity.

Article Tags :