jQuery is a fast, lightweight JavaScript library that simplifies tasks like DOM manipulation, event handling, animations, and AJAX. It’s widely used in web development to create interactive and dynamic websites.
Table of Contents
ToggleIn this tutorial, we’ll cover essential jQuery topics frequently used in web development.
Getting Started with jQuery
How to Include jQuery
To use jQuery, add the following <script>
tag in your HTML file:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Frequently Used jQuery Features
1. DOM Manipulation
Select Elements
$("p"); // Select all <p> elements
$(".className"); // Select elements by class
$("#idName"); // Select an element by ID
Modify Content
$("#element").text("New text content"); // Change text
$("#element").html("<b>New HTML content</b>"); // Change HTML
$("#input").val("New Value"); // Set value for input fields
Modify Attributes
$("#link").attr("href", "https://example.com"); // Change attribute
Add or Remove Elements
$("#list").append("<li>New Item</li>"); // Add at the end
$("#list").prepend("<li>New Item</li>"); // Add at the beginning
$("#item").remove(); // Remove element
2. CSS Manipulation
Add/Remove CSS Classes
$("#element").addClass("newClass"); // Add class
$("#element").removeClass("oldClass"); // Remove class
$("#element").toggleClass("activeClass"); // Toggle class
Direct CSS Styling
$("#element").css("color", "red"); // Change CSS property
$("#element").css({
"background-color": "blue",
"font-size": "16px"
}); // Change multiple properties
3. Event Handling
Common Events
$("#btn").click(function() {
alert("Button clicked!");
});
$("#input").keyup(function() {
console.log($(this).val());
});
$("#div").hover(
function() {
$(this).css("background-color", "yellow"); // Mouse Enter
},
function() {
$(this).css("background-color", "white"); // Mouse Leave
}
);
Delegate Events
Use on()
to handle dynamically added elements:
$("body").on("click", ".dynamicElement", function() {
alert("Dynamic element clicked!");
});
4. Animations and Effects
Show/Hide Elements
$("#element").hide(); // Hide instantly
$("#element").show(); // Show instantly
$("#element").toggle(); // Toggle visibility
Fading Effects
$("#element").fadeIn(); // Fade in
$("#element").fadeOut(); // Fade out
$("#element").fadeToggle(); // Toggle fade
Sliding Effects
$("#panel").slideUp(); // Slide up
$("#panel").slideDown(); // Slide down
$("#panel").slideToggle(); // Toggle slide
Custom Animations
$("#box").animate({
width: "300px",
opacity: 0.5
}, 1000); // Animate with duration in ms
5. AJAX with jQuery
Load Data from Server
$("#result").load("example.html");
Perform AJAX Requests
$.ajax({
url: "https://api.example.com/data",
method: "GET",
success: function(data) {
console.log(data);
},
error: function(error) {
console.error("Error:", error);
}
});
Shorthand Methods
$.get("example.html", function(data) {
$("#result").html(data);
});
$.post("submit.php", { name: "John" }, function(response) {
console.log(response);
});
6. Traversing the DOM
Parent, Child, Siblings
$("#child").parent(); // Select parent
$("#parent").children(); // Select children
$("#item").siblings(); // Select siblings
Filtering
$("li").first(); // Select first element
$("li").last(); // Select last element
$("li").eq(2); // Select third element (zero-indexed)
7. Form Handling
Get/Set Form Values
var value = $("#input").val(); // Get value
$("#input").val("New Value"); // Set value
Handle Form Submission
$("#form").submit(function(e) {
e.preventDefault(); // Prevent default submission
alert("Form submitted!");
});
8. Utilities
Check if Element Exists
if ($("#element").length) {
console.log("Element exists");
}
Iterate Over Elements
$("li").each(function(index) {
console.log("Item " + index + ": " + $(this).text());
});
Delay Execution
$("#element").delay(1000).fadeOut(); // Wait 1 second before fading
Complete Example: Interactive Web Page
Here’s an example that combines multiple jQuery features:
<!DOCTYPE html>
<html>
<head>
<title>jQuery Interactive Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#box { width: 100px; height: 100px; background: lightblue; margin: 20px; }
</style>
</head>
<body>
<button id="toggleBtn">Toggle Box</button>
<button id="animateBtn">Animate Box</button>
<div id="box"></div>
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<button id="addItem">Add Item</button>
<script>
$(document).ready(function() {
// Toggle box visibility
$("#toggleBtn").click(function() {
$("#box").toggle();
});
// Animate the box
$("#animateBtn").click(function() {
$("#box").animate({ width: "200px", height: "200px" }, "slow");
});
// Add new item to the list
$("#addItem").click(function() {
$("#list").append("<li>New Item</li>");
});
});
</script>
</body>
</html>
jQuery is a must-know library for web development, especially for beginners. It simplifies complex tasks and allows you to create dynamic, interactive websites effortlessly.