Example 1 — Add One Class
Official API idea: add selected to the last paragraph.
// <p>Hello</p><p>and</p><p>Goodbye</p>
$("p").last().addClass("selected");
console.log($("p").last().attr("class")); // "selected" How It Works
- Select all.
$("p")matches every paragraph. - Narrow.
.last()keeps only the final match. - Append.
.addClass("selected")writes that name onto the class attribute.

