12 problems — selectors, content, styling, events, animations & more
Practice: ID selector + .text()
headline and change its text to "jQuery is awesome!"$("#your-id").text("your text here")$("#headline").text("...")$("#headline").text("jQuery is awesome!");
This paragraph should stay the same.
Practice: class selector + .css()
card and change their background color to #DBEAFE#1E40AF$(".class-name").css("property", "value").css({ "property": "value", "property2": "value2" })$(".card").css({
"background": "#DBEAFE",
"color": "#1E40AF"
});
Practice: .val() + console.log()
username and store it in a variableHello, Ada#greeting-output to show the same messagevar name = $("#username").val();"Hello, " + nameconsole.log("Hello, " + name);$("#greeting-output").text("Hello, " + name);var name = $("#username").val();
console.log("Hello, " + name);
$("#greeting-output").text("Hello, " + name);
Practice: .html() vs .text()
#status-box to: <strong>✅ Connected</strong> — server is online#warning-box to: <em style="color:#DC2626">⚠️ Low battery</em>.html("...") renders HTML tags — use this instead of .text() when you want formatting$("#box").html("<strong>Bold!</strong>")$("#status-box").html("<strong>✅ Connected</strong> — server is online");
$("#warning-box").html("<em style='color:#DC2626'>⚠️ Low battery</em>");
Practice: .click() + .show() / .hide()
#reveal-btn is clicked, show the element #secret-panel#hide-btn is clicked, hide #secret-panel again$("#btn").click(function() { ... });$("#element").show();$("#element").hide();$("#reveal-btn").click(function() {
$("#secret-panel").show();
});
$("#hide-btn").click(function() {
$("#secret-panel").hide();
});
You revealed the secret panel!
Practice: .addClass() · .removeClass() · .toggleClass()
#add-btn is clicked, add class highlighted to #demo-box#remove-btn is clicked, remove class highlighted from #demo-box#toggle-btn is clicked, toggle class highlighted on #demo-box.addClass("highlighted"), .removeClass("highlighted"), and .toggleClass("highlighted").
The .highlighted class is already defined in the preview — it turns the box gold.
$("#add-btn").click(function() {
$("#demo-box").addClass("highlighted");
});
$("#remove-btn").click(function() {
$("#demo-box").removeClass("highlighted");
});
$("#toggle-btn").click(function() {
$("#demo-box").toggleClass("highlighted");
});
Practice: .hover() + $(this)
.menu-item, change its background to #2563EB and text color to white#F1F5F9 and color to #1E293B.hover(fnEnter, fnLeave) takes two functions — one for in, one for out$(this) inside the handler to reference the hovered element.menu-item elements — jQuery handles this automatically$(".menu-item").hover(
function() {
$(this).css({ "background": "#2563EB", "color": "white" });
},
function() {
$(this).css({ "background": "#F1F5F9", "color": "#1E293B" });
}
);
Practice: .click() + .val() + .append()
#add-task-btn is clicked, read the value from #task-input<li>[task]</li> to #task-listif (value !== "") { ... }"<li>" + value + "</li>".val("")$("#add-task-btn").click(function() {
var task = $("#task-input").val();
if (task !== "") {
$("#task-list").append("<li style='padding:8px;border-bottom:1px solid #E2E8F0'>" + task + "</li>");
$("#task-input").val("");
}
});
Practice: .fadeOut() · .fadeIn() · .remove()
#fade-out-btn is clicked, fade out #fade-target#fade-in-btn is clicked, fade it back in.dismiss-btn is clicked, completely remove its parent .alert-card from the DOM.fadeOut() and .fadeIn() animate opacity$(this).parent().remove() deletes an element from the DOM entirely$("#fade-out-btn").click(function() {
$("#fade-target").fadeOut();
});
$("#fade-in-btn").click(function() {
$("#fade-target").fadeIn();
});
$(".dismiss-btn").click(function() {
$(this).parent().remove();
});
Dismiss these alerts:
Practice: $(this) · .siblings() · chaining
.option-card is clicked, add class selected to it and remove selected from all its sibling .option-card elements#chosen-label's text to show which option was selected, using the clicked card's .find(".option-name") text$(".option-card").click(function() {
$(this).addClass("selected")
.siblings(".option-card").removeClass("selected");
var name = $(this).find(".option-name").text();
$("#chosen-label").text("You chose: " + name);
});
Practice: .prepend() · .last() · .remove() · live counters
#prepend-btn is clicked, prepend a new item <li>New item</li> to #dyn-list#remove-last-btn is clicked, remove the last <li> in #dyn-list#item-count's text to show the current number of li children$("#prepend-btn").click(function() {
$("#dyn-list").prepend("<li style='padding:8px;border-bottom:1px solid #E2E8F0'>New item</li>");
$("#item-count").text($("#dyn-list li").length);
});
$("#remove-last-btn").click(function() {
if ($("#dyn-list li").length > 0) {
$("#dyn-list li").last().remove();
$("#item-count").text($("#dyn-list li").length);
}
});
No hints — put it all together
.like-btn is clicked, read its current data-count attribute, increment it by 1, and write it back.like-count child span to display the new numberliked on the button (the style is already defined — it turns the button blue)#total-likes to always show the sum of all buttons' data-count values$(".like-btn").click(function() {
var count = parseInt($(this).attr("data-count")) + 1;
$(this).attr("data-count", count);
$(this).find(".like-count").text(count);
$(this).toggleClass("liked");
var total = 0;
$(".like-btn").each(function() {
total += parseInt($(this).attr("data-count"));
});
$("#total-likes").text(total);
});
Just deployed my first jQuery project! 🚀
Finally understood what $(this) means 😅
CSS animations + jQuery = 🔥