jQuery Tutorial: Basics, Tricks & Advanced Techniques
1. Introduction to jQuery
jQuery is a fast, lightweight, and feature-rich JavaScript library that simplifies HTML DOM
manipulation, event handling, animations, and AJAX.
Why Use jQuery?
• Simplifies JavaScript code.
• Makes event handling easier.
• Provides cross-browser compatibility.
• Comes with built-in animations and AJAX support.
How to Include jQuery?
To use jQuery, add the following CDN link inside the <head> section:
<script src="[Link]
2. jQuery Syntax
The basic syntax for jQuery is:
$(selector).action();
Example:
$(document).ready(function() {
$("button").click(function() {
$("p").hide();
});
});
3. jQuery Selectors (Tricks)
jQuery allows selecting elements just like CSS:
$("p:first") → Selects the first <p> element.
$("p:last") → Selects the last <p> element.
$("p:even") → Selects even <p> elements.
$("p:odd") → Selects odd <p> elements.
4. jQuery Event Handling (Advanced)
Common Events include click, hover, dblclick, and keypress.
Example:
$("#btn").on("click dblclick", function() {
alert("Clicked or Double-clicked!");
});
5. jQuery Effects & Animations
Basic effects include fadeIn, fadeOut, slideUp, slideDown, and toggle.
Example:
$("#box").fadeIn(2000).fadeOut(2000);
$("#box").slideUp().slideDown();
6. jQuery DOM Manipulation
Changing Content Example:
$("#title").text("New Title");
$("#content").html("<b>Bold Text</b>");
7. jQuery AJAX Requests (Advanced)
Example of loading content dynamically:
$("#data").load("[Link]");
8. jQuery Chaining (Pro Trick)
Example:
$("#box").css("background", "blue").slideUp(2000).slideDown(2000);
9. jQuery Form Handling
Example of getting form values:
$("#submitBtn").click(function() {
var name = $("#nameInput").val();
alert("Hello, " + name);
});
10. jQuery Performance Tips
1. Use `this` for efficiency
2. Minimize DOM access
3. Cache selectors
11. Conclusion
This guide covered jQuery basics, tricks, advanced techniques, and performance tips. Happy
coding! 🚀