JQuery

Introduction:

jQuery is a “write less, do more” JavaScript library. It’s not a programming language, it’s a tool for writing more JavaScript functions. jQuery has the advantage of being cross-browser compatible; this means you can be sure that the output of your code will work as expected in all modern browsers.

To use jQuery, you must include the jQuery library in your HTML file. You can either download the library and host it locally or use a CDN (Content Delivery Network) to load it from a remote server.

Here is an example of jQuery from a Content Delivery Network:

<html>

<head>

<title>My jQuery Example</title>

<script src=”https://code.jquery.com/jquery-3.6.0.min.js”></script>

</head>

<body>

<!– Your HTML content here –>

</body>

</html>

Once jQuery is included, you can start using its features.

Selecting Elements:

One of the most important features of jQuery is its powerful selector engine, which allows you to easily select and edit HTML elements. jQuery uses CSS-like selectors to select elements. Here is an example that selects and hides all paragraphs () in a document:

$(document).ready(function() {

  // jQuery code goes here

  $(“p”).hide();

});

DOM Manipulation:

jQuery provides a wide range of methods for manipulating the Document Object Model (DOM). You can use these methods to add, remove, modify, or animate HTML elements. Here is an example that adds a new element named ID “myDiv” to a:

$(document).ready(function() {

  // jQuery code goes here

¬† $(“#myDiv”).append(“<p>Hello, jQuery!</p>”); });

Event Handling:

jQuery simplifies event handling by providing a concise syntax for attaching event listeners to elements. Here’s an example that alerts a message when a button with the ID “myButton” is clicked:

$(document).ready(function() {

  // jQuery code goes here

  $(“#myButton”).click(function() {

    alert(“Button clicked!”);

  });

});

AJAX Requests:

jQuery’s AJAX module makes it easy to perform asynchronous HTTP requests and handle the responses. It provides methods like $.ajax() and shorthand methods such as $.get() and $.post(). Here’s an example that fetches data from a remote server and updates a <div> with the response:

$(document).ready(function() {

  // jQuery code goes here

  $.get(“https://api.example.com/data”, function(response) {

    $(“#myDiv”).text(response);

  });

});

AJAX and Asynchronous Communication:

Another notable feature of jQuery is its support for AJAX, which allows you to make asynchronous requests to a server without refreshing the entire page. This capability is especially useful when you want to dynamically load data or submit forms without having to reload the page.

Here’s an example of making an AJAX request using jQuery, when you click the button square size will be increased with the response:

<html>

<head>

<meta charset=”utf-8″>

<title>Example of jQuery Animation with Relative Values</title>

<script src=”https://code.jquery.com/jquery-3.5.1.min.js”></script>

<style>

    .box{

        width: 100px;

        height: 100px;

        background: #085078;

        margin-top: 30px;

        position: relative; /* Required to move element */

    }

  .center {

  margin: 0;

  position: absolute;

  top: 20px;

  left: 50%;

  -ms-transform: translate(-50%, -50%);

  transform: translate(-50%, -50%);

}

</style>

<script>

$(document).ready(function(){

    $(“button”).click(function(){

        $(“.box”).animate({           

            top: “+=50px”,

            left: “+=50px”,

            width: “+=50px”,

            height: “+=50px”

        });

    });

});

</script>

</head>

<body>

  <button type=”button” class=”center”>Click To Resize</button><br/>

    <p><strong>Note:</strong> When You Click The Button Square Size Will Be Increased.</p>

    <div class=”box”></div>

</body>

</html>

Output:

Conclusion:

jQuery is a powerful JavaScript library that simplifies web development by providing a wide range of features and utilities. In this blog post, we covered the basics of jQuery and demonstrated its capabilities with two examples: handling click events and dynamically modifying HTML elements. By leveraging jQuery’s concise syntax and extensive API, you can streamline your web development process and create more interactive and responsive websites.