jQuery is a fast, lightweight, and feature-rich JavaScript library designed to simplify HTML DOM manipulation, event handling, animations, and AJAX interactions. It allows developers to write less code while achieving more functionality.
jQuery uses a simple and concise syntax. Most jQuery code starts with the $() function, which selects HTML elements and performs actions on them.
// Basic jQuery syntax structure
$(selector).action();
// Example: Wait for document to load
$(document).ready(function(){
// jQuery methods go here...
});
One of jQuery's strongest features is its engine to find elements in the DOM tree.
| Selector Type | Syntax | Description |
|---|---|---|
| Element | $("p") |
Selects all <p> elements. |
| ID | $("#test") |
Selects the element with id="test". |
| Class | $(".active") |
Selects all elements with class="active". |
| This | $(this) |
Selects the current HTML element. |
| Traversal | .parent() |
Finds the direct parent of the element. |
$.get() or $.post().
// Changing text and adding a class using jQuery
$("#title").text("Hello jQuery").addClass("highlight");
The selector #title selects an element by ID. The text() method changes its content, and addClass() applies a CSS class — all in one chained statement.
// Button click toggles paragraph visibility
$("#toggleBtn").click(function() {
$("#box").slideToggle();
});
Click the buttons below to see jQuery in action on the box below.
$(document).ready() to ensure DOM is loadedvar $btn = $("#btn");) for better performancefadeIn() and fadeOut() effectsanimate()$.ajax()