topskill.dev
CoursesTracks
0
All courses/DOM Manipulation with JavaScriptAdd and remove community directory cards
Project — Community Directory
6 / 7
← DOM Manipulation with JavaScript
○Open the browser Console safely○Read results, fix a typo, and try again○Change your blank practice page○The page is a tree○follow the document reference○Load a script when the page is ready○initialize a deferred script○Find one element by id or CSS selector○select a notice and label○Compare DOM properties, HTML attributes, and node types○inspect a room input and its nodes○Initialize a page when it is ready✦Module Quiz
○Find every match with querySelectorAll○label every trail stop○Move from a child to its relatives○trace a nested card○Check for missing elements and search within a card○update only the chosen card○Compare a live HTMLCollection with a static NodeList○safely expand a live collection✦Module Quiz
○Safe text and HTML○render a safe notice○Read and change link attributes○update an accessible link○Data attributes and conditional text○announce session availability○Boolean attributes and DOM properties○unlock a volunteer sign-up✦Module Quiz
○Add, remove, and check classes○mark a favourite card○Toggle a panel accessibly○toggle the help panel○Inline styles, classes, and hidden○style and hide a notice○Change a CSS custom property from JavaScript○change a workshop card accent✦Module Quiz
○Create an Element and Append It○Add a Reading List Item○Place Nodes Before and Between○Order the Workshop Agenda○Remove, Replace, and Clear Safely○Refresh a Museum Display○Clone a Card and Insert a Batch○Clone Two Exhibit Cards✦Module Quiz
○Listen for Clicks with a Named Handler○Check Library Hours○Event Target, Current Target, and Delegation○Delegate Pantry Actions○Keyboard Events Without Trapping Keys○Search the Recipe Box○Once, Remove, and Bubble○Control Workshop Notifications✦Module Quiz
○Read a field while someone types○Build a live name preview○Handle submit without leaving the page○Validate a library request○Keep a selection in sync on change and reset○Sync a preference and its reset state○Read multiple checked values with FormData○Build a checkbox-based garden request○Read a number from a form✦Module Quiz
○Filter a list as someone types○Build a live trail finder○Toggle an accessible disclosure○Build a guide menu disclosure○Create and remove items with one delegated listener○Build a neighborhood task board○Sort a workshop table with accessible buttons○Build an accessible sortable resource table✦Module Quiz
○Read selectable cards and detail updates○Build directory cards and a selected profile○Trace live search and an empty state○Add live search to the community directory○Trace safe additions and delegated removal●Add and remove community directory cards✦Module Quiz
○Read and update a live itinerary○Build the workshop itinerary○Add a validated item through a form○Add a workshop session○Filter and remove live entries○Finish the workshop planner✦Module Quiz
○Fetch JSON and render safe text○load recipe cards○Handle loading, empty, and failed responses○handle alert states○Build a query with URLSearchParams○search the catalog○Abort outdated requests○keep the latest result✦Module Quiz
○Load a dashboard from an offline API fixture○Build an events dashboard○Query a dashboard with URLSearchParams○Add event search to the dashboard○Retry requests without stale results○Finish a resilient event dashboard✦Module Quiz

Add and remove community directory cards

Finish the community directory by adding members through a labelled form and removing old or new cards. The starter is the completed selection-and-search step, with a form and Remove buttons in its HTML; those new controls have no handlers yet. The seed-swap demo showed the event flow using different elements.

Create DOM nodes for submitted data. Put untrusted names and skills into textContent, not innerHTML. Listen for removals on the stable #members parent, not just its initial buttons. Re-run the existing filter after any addition or removal so the count and empty state remain truthful. Filtering already clears details when it hides the selected card; removing a selected card must also clear its stale detail view. The existing View handler will also work for new cards because it listens on the same parent.

Here is a smaller example from a different project: a reading log can create a title safely and hear a Remove click from a button added later.

javascript
const entry = document.createElement("li");
const title = document.createElement("span");
title.textContent =
  document.querySelector("#book-title").value;
entry.append(title);
const remove = document.createElement("button");
remove.type = "button";
remove.className = "remove-book";
remove.textContent = "Remove";
entry.append(remove);
document.querySelector("#reading-log").append(entry);

document.querySelector("#reading-log")
  .addEventListener("click", (event) => {
    const button =
      event.target.closest("button.remove-book");
    if (button && event.currentTarget.contains(button)) {
      button.closest("li").remove();
    }
  });

Your directory needs this safe creation and parent-listener pattern plus buttons, selection details, form feedback, and the existing search. If a selected card disappears, clear its details rather than describing someone who is no longer there.

The starter already includes the labelled form and original Remove buttons. Its delegated View listener also works on newly created cards; leave those in place. createMemberCard prepares a node without inserting it, so you can test that helper before wiring submission. Edit script.js, Run to inspect each change, then Submit. The reference resets the form and focuses the name field after a successful addition; this convenience does not reset the active search.

This directory permits matching names, because two neighbors may share a name. Each submission creates a separate card; removal acts on the clicked card, not the first matching name. A filter stays active after additions, so a new member may be present but hidden.

Your Task

  1. Prevent default navigation whenever #member-form is submitted.
  2. Reject a blank or whitespace-only name or skill with feedback and no new card.
  3. Complete createMemberCard(name, skill) to return one detached card with literal name and skill text, matching data attributes, a View button, and a named Remove button.
  4. On valid submission, append the card returned by your helper to #members.
  5. Reapply the current search after an addition so the new card’s visibility, result count, and empty state agree.
  6. Announce the new member in #form-status after adding the card.
  7. Delegate Remove clicks on #members for both original and future cards, including clicks inside a button.
  8. Announce the removed member in #form-status.
  9. If the removed member was selected, restore the default name and skill details. Removing another member must keep the current details.
  10. After removal, refresh the result count and empty state for the current query.
  11. After removal, focus #member-search so keyboard users can continue.

Try adding <em>Ada</em> with the skill “Painting.” Search for painting, view the new card, remove it, then clear the search. The markup remains literal, the detail resets, and the original cards return. Your directory now supports selection, search, additions, and removal.

Take the Quiz →
Files
  • ⋄index.html
  • ⋄script.js
  • ⋄style.css
Loading editor…