How structure and style work together
.card background color, or change the button text in the HTML. Hit "Update Preview" to see your changes!
Changing content dynamically requires a lot of code
document.getElementById() and document.getElementsByClassName() — long method namesfor loop just to change multiple elementscreateElement, set properties, then appendChildA fast, small JavaScript library that simplifies web development
Select any element on the page using CSS-style selectors — by ID, class, tag, or complex combinations.
Read or modify text, HTML, attributes, and form values with simple one-line commands.
Change CSS properties, add/remove classes, and toggle styles dynamically.
Respond to clicks, hovers, key presses, and other user interactions easily.
Show, hide, fade, and slide elements with built-in animation methods.
Fetch data from servers without reloading the page — simplified HTTP requests.
jQuery released - revolutionary for its time. Made cross-browser development possible.
Dominated web development. Used on 70%+ of all websites.
React, Vue, Angular emerge. Modern browsers become consistent.
Still on ~77% of sites (legacy), but rarely chosen for new projects.
querySelector, fetch, classList$ is just an alias for jQuery. When you see $("...") in code, that's jQuery!
// All that vanilla JS... becomes this:
$("#main-heading").text("New Title!");
$(".item").css({"color": "blue", "font-weight": "bold"});
$("#container").append("<p style='background:#10B981;color:white;padding:10px'>Added!</p>");
3 lines instead of 15+! That's the power of jQuery.
The foundation of jQuery - selecting elements
$("selector")| Selection Type | jQuery | Vanilla JS |
|---|---|---|
| By ID | $("#myId") | document.getElementById("myId") |
| By Class | $(".myClass") | document.querySelectorAll(".myClass") |
| By Tag | $("div") | document.querySelectorAll("div") |
$("#demo-box") — Selects the element with ID "demo-box" and adds a red border$(".demo-item") — Selects ALL elements with class "demo-item" and changes their text to blue.length — Returns how many elements were found (useful for debugging)Reading and changing element content
| Method | Description | Vanilla JS |
|---|---|---|
.html() |
Get/set HTML content (parses tags) | innerHTML |
.text() |
Get/set text only (ignores tags) | textContent |
.val() |
Get/set form input values | value |
.html("<strong>...</strong>") — Replaces content and renders the HTML tags.text("<em>...</em>") — Replaces content but shows tags as plain text.val() — Gets the current input value; .val("new") sets itChanging CSS and toggling classes
| Method | Description |
|---|---|
.css("property", "value") |
Set a single CSS property |
.css({prop1: val1, prop2: val2}) |
Set multiple CSS properties at once |
.addClass() / .removeClass() |
Add or remove a CSS class |
.toggleClass() |
Add class if missing, remove if present |
.css({...}) — Applies multiple inline styles to the element.addClass("highlight") — Adds a predefined CSS class.toggleClass("active") — Run multiple times to see it toggle on/offAdding and removing elements from the page
| Method | Description |
|---|---|
.append(content) |
Add inside element, at the end |
.prepend(content) |
Add inside element, at the start |
.after(content) |
Add outside, immediately after |
.remove() |
Delete the element entirely |
.append() — Adds a new list item at the end of the list.prepend() — Adds a new list item at the beginning$("<div>") — Creates a brand new element that you can then insertTry it yourself! Complete the tasks below.
greeting and change its text to "Hello, jQuery!"box and give them a blue background (#3B82F6)special and add the class highlight$("#id") to select by ID$(".class") to select by class.text("...") to change text.css("property", "value") to change styles.addClass("className") to add a class// Task 1
$("#greeting").text("Hello, jQuery!");
// Task 2
$(".box").css("background", "#3B82F6");
// Task 3
$("#special").addClass("highlight");
Manipulate content and add new elements!
username and log it to the console#message to: <strong>Updated!</strong><li>Item 4</li> to the end of #todo-list.val() to get input values.html("...") to set HTML content (renders tags).append("...") to add content at the endconsole.log(...) to output to the console// Task 1
var name = $("#username").val();
console.log("Username:", name);
// Task 2
$("#message").html("<strong>Updated!</strong>");
// Task 3
$("#todo-list").append("<li>Item 4</li>");
Responding to user interactions
| Method | Description |
|---|---|
.on("click", fn) |
Attach any event handler (preferred method) |
.click(fn) |
Shorthand for click events |
.hover(fnIn, fnOut) |
Handle mouseenter and mouseleave |
$(this) |
Reference to the element that triggered the event |
.click(function() {...}) — Runs the function when element is clicked$(this) — Inside the handler, refers to the clicked element.hover() — First function runs on mouse enter, second on mouse leaveNavigating between related elements
| Method | Description |
|---|---|
.parent() |
Select the direct parent element |
.children() |
Select all direct children |
.siblings() |
Select all sibling elements |
.find("selector") |
Search within descendants |
.first() / .last() / .eq(n) |
Select specific element from a set |
.first() / .last() — Selects first or last element from matched set.eq(1) — Selects element at index 1 (zero-based, so second item).find("li") — Searches for all <li> elements inside the containerBuilt-in effects for showing, hiding, and transitioning
| Method | Description |
|---|---|
.show() / .hide() / .toggle() |
Display or hide elements instantly or with duration |
.fadeIn() / .fadeOut() / .fadeToggle() |
Animate opacity |
.slideDown() / .slideUp() / .slideToggle() |
Animate height |
.animate({props}, duration) |
Custom animations with any CSS properties |
"slow", "fast", or milliseconds like 500.animate() lets you animate any numeric CSS propertySide-by-side comparison — everything jQuery does, vanilla JS can do too
| Task | jQuery | Modern Vanilla JS |
|---|---|---|
| Select element | $("#id") | document.querySelector("#id") |
| Select all | $(".class") | document.querySelectorAll(".class") |
| Add class | $(el).addClass("x") | el.classList.add("x") |
| Set CSS | $(el).css("color", "red") | el.style.color = "red" |
| Set HTML | $(el).html("...") | el.innerHTML = "..." |
| Click event | $(el).click(fn) | el.addEventListener("click", fn) |
| AJAX request | $.get(url, callback) | fetch(url).then(r => r.json()) |
Ensuring the DOM is loaded before your code runs
| Syntax | Description |
|---|---|
$(document).ready(function() {...}) |
Full syntax — runs when DOM is ready |
$(function() {...}) |
Shorthand — same thing, less typing |
document.addEventListener("DOMContentLoaded", function() {...}) — or simply place your <script> tag at the end of <body>.
Make the page interactive!
#color-btn is clicked, change #color-box background to green (#10B981)#fade-btn is clicked, fade out #fade-box.list-item is clicked, add the class selected to it.click(function() { ... }) to handle clicks.fadeOut() to fade an element$(this) inside an event handler to refer to the clicked element// Task 1
$("#color-btn").click(function() {
$("#color-box").css("background", "#10B981");
});
// Task 2
$("#fade-btn").click(function() {
$("#fade-box").fadeOut();
});
// Task 3
$(".list-item").click(function() {
$(this).addClass("selected");
});
Click items to select:
Combine everything you've learned!
#add-btn is clicked, get the text from #product-name#cart-list.val() to get input text"<li>" + variable + "</li>".append() to add to the list.val("") to clear an input$("#add-btn").click(function() {
// 1. Get the product name
var product = $("#product-name").val();
// 2 & 3. Create and append new list item
$("#cart-list").append("<li>" + product + "</li>");
// 4. Clear the input
$("#product-name").val("");
});