Aral Code

Changing the Page Lesson 7 of 10

jQuery — Adding, Removing and Styling Elements

Create and insert elements with append, prepend, before and after, remove and empty them, change styles with css, and toggle classes with addClass, removeClass and toggleClass.

jQuery makes it easy to build new elements, put them anywhere on the page, remove them, and change how they look.

Creating and inserting

Example append, prepend, before, after
<script src="/vendor/jquery.min.js"></script>
<ul id="queue"><li>Ben</li></ul>
<button id="a">append</button> <button id="p">prepend</button>
<button id="b">before list</button> <button id="af">after list</button>
<script>
  let n = 1;
  $("#a").on("click", () => $("#queue").append("<li>Last #" + n++ + "</li>"));
  $("#p").on("click", () => $("#queue").prepend("<li>First #" + n++ + "</li>"));
  $("#b").on("click", () => $("#queue").before("<p>⬆ Added before the list</p>"));
  $("#af").on("click", () => $("#queue").after("<p>⬇ Added after the list</p>"));
</script>
MethodInserts
.append(x)Inside, at the end
.prepend(x)Inside, at the start
.after(x)Outside, right after
.before(x)Outside, right before

Building elements safely

For content that includes user input, create the element first and set its text:

const name = $("#nameInput").val();
const li = $("<li>").text(name).addClass("student");
$("#list").append(li);

$("<li>") creates a new element; .text(name) inserts the name safely.

Removing

$("#notice").remove();     // removes the element itself
$("#list").empty();        // removes everything inside, keeps the list

Changing styles: css()

Example Reading and setting CSS
<script src="/vendor/jquery.min.js"></script>
<p id="txt" style="font-size:16px">Aral Code</p>
<button id="bigger">A+</button> <button id="smaller">A−</button> <button id="color">Color</button>
<script>
  $("#bigger").on("click", function () {
    const size = parseInt($("#txt").css("font-size"));
    $("#txt").css("font-size", size + 2 + "px");
  });
  $("#smaller").on("click", function () {
    const size = parseInt($("#txt").css("font-size"));
    $("#txt").css("font-size", Math.max(10, size - 2) + "px");
  });
  $("#color").on("click", function () {
    $("#txt").css({ color: "white", background: "#0f5c4d", padding: "6px 10px" });
  });
</script>

Better: classes

Instead of setting many styles in JavaScript, define them in CSS and switch classes:

Example addClass, removeClass, toggleClass
<script src="/vendor/jquery.min.js"></script>
<style>
  body { font-family: sans-serif; transition: background .3s, color .3s; }
  body.dark { background: #161b1a; color: #e8efec; }
  .task.done { text-decoration: line-through; opacity: .5; }
  .task { cursor: pointer; }
</style>
<button id="theme">🌙 Dark mode</button>
<ul>
  <li class="task">Assignment sa Math</li>
  <li class="task">Review for quiz</li>
  <li class="task">Linis ng kwarto</li>
</ul>
<p>Click a task to mark it done.</p>
<script>
  $("#theme").on("click", function () {
    $("body").toggleClass("dark");
    $(this).text($("body").hasClass("dark") ? "☀️ Light mode" : "🌙 Dark mode");
  });
  $(".task").on("click", function () {
    $(this).toggleClass("done");
  });
</script>
  • .addClass("x"), .removeClass("x"), .toggleClass("x")
  • .hasClass("x") returns true or false

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which method adds content inside an element, at the end?
2. What does .empty() do?
3. Which method switches a class on and off?
4. How do you create a new <li> element with jQuery?

By Sir Reyel · Last updated 2026-12-04