If you’ve been learning HTML and CSS for a while, you’ve probably realized that they can make a website look great, but they can’t make it do much. That’s where JavaScript comes in. And if you want to make JavaScript easier to use—especially when dealing with HTML elements—jQuery is a fantastic tool.
In this post, we’ll go through the basics of jQuery, how to set it up, and write your first interactive code.
What is jQuery?
jQuery is a lightweight JavaScript library that makes it easier to:
-
Select and manipulate HTML elements
-
Handle events (clicks, hovers, etc.)
-
Create animations and effects
-
Work with AJAX requests
Its motto is:
“Write less, do more.”
Step 1: Setting Up jQuery
You can add jQuery to your project in two main ways:
Option 1: Use a CDN (Recommended)
Add this line before your closing </body>
tag:
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
Option 2: Download and Host It Yourself
-
Download the
.js
file -
Include it in your project like:
<script src="js/jquery.min.js"></script>
Step 2: Your First jQuery Script
Here’s a simple example that changes the text of a paragraph when a button is clicked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html> <head> <title>jQuery Example</title> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <p id="demo-text">Hello, world!</p> <button id="change-btn">Click Me</button> <script> $(document).ready(function() { $("#change-btn").click(function() { $("#demo-text").text("You clicked the button!"); }); }); </script> </body> </html> |
How it works:
-
$(document).ready()
makes sure the code runs after the page loads. -
$("#change-btn")
selects the button with IDchange-btn
. -
.click(function(){ ... })
sets what happens when the button is clicked. -
.text("...")
changes the text of the paragraph.
Step 3: Common jQuery Selectors
Selectors are the core of jQuery—they let you grab elements and apply actions to them.
Selector | Example | What it does |
---|---|---|
#id | $("#myDiv") | Selects the element with the given ID |
.class | $(".myClass") | Selects all elements with that class |
element | $("p") | Selects all <p> elements |
* | $("*") | Selects all elements |
parent child | $("div p") | Selects <p> elements inside <div> |
Step 4: jQuery Events
Here are a few common events you can use:
1 2 3 4 5 6 7 8 9 10 11 | $("button").click(function() { alert("Button clicked!"); }); $("p").dblclick(function() { $(this).hide(); }); $("#myInput").keyup(function() { console.log($(this).val()); }); |
Step 5: Adding Simple Effects
Want to make things more fun? jQuery has built-in effects:
1 2 3 4 5 6 7 | $("#fade-btn").click(function() { $("#box").fadeOut(); }); $("#slide-btn").click(function() { $("#panel").slideToggle(); }); |
Step 6: Chaining Methods
One of the coolest jQuery features is method chaining:
$("#box").css("color", "red").slideUp(500).slideDown(500);
This changes the text color, slides the element up, then slides it back down—all in one line.
Why Use jQuery in 2025?
Even though modern JavaScript (ES6+) has caught up with many jQuery features, it’s still:
-
Easy to learn for beginners
-
Widely used in legacy projects
-
Handy for quick prototypes
Final Tips
-
Always place your jQuery scripts after the HTML elements they affect, or wrap them in
$(document).ready()
. -
Practice by making small interactive elements before diving into complex projects.
-
Once you’re comfortable, explore AJAX, animations, and plugins.
✅ Now you know the basics of jQuery—from setup to selectors, events, and effects. Start experimenting, and you’ll quickly make your static pages come alive.
jQuery Cheat Sheet (Quick Reference)
Action | jQuery Code | Description |
---|---|---|
Document Ready | $(document).ready(fn) | Runs code after the DOM is loaded |
Select by ID | $("#id") | Selects element by ID |
Select by Class | $(".class") | Selects elements by class |
Select by Tag | $("p") | Selects all <p> elements |
Click Event | $("selector").click(fn) | Runs code when clicked |
Double Click | $("selector").dblclick(fn) | Runs code when double-clicked |
Change Text | $("selector").text("New") | Changes element text |
Change HTML | $("selector").html("<b>Hi</b>") | Changes inner HTML |
Change CSS | $("selector").css("color", "red") | Changes style |
Hide Element | $("selector").hide() | Hides element |
Show Element | $("selector").show() | Shows element |
Toggle Visibility | $("selector").toggle() | Shows if hidden, hides if shown |
Fade In | $("selector").fadeIn() | Fades element in |
Fade Out | $("selector").fadeOut() | Fades element out |
Slide Down | $("selector").slideDown() | Slides element down |
Slide Up | $("selector").slideUp() | Slides element up |
Get Value | $("selector").val() | Gets form field value |
Set Value | $("selector").val("Hello") | Sets form field value |
✅ With this cheat sheet, you can quickly try out common jQuery actions without memorizing every syntax. Practice small examples, then combine them for more advanced interactivity.
No comments:
Post a Comment