Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Add and Remove DOM Elements | DOM Manipulation for Interactive Web Development
Advanced JavaScript Mastery

book
Challenge: Add and Remove DOM Elements

Task

You're building a shopping cart where users can add and remove items.

  1. Add an Item to the Cart:

    • Create a new <li> element;

    • Create a new <button> element;

    • Add new <button> element to the new <li> element;

    • Add a new <li> element to the <ul> with the cart-list ID.

  2. Remove an Item from the Cart: Delete the specific <li> element from the <ul>.

html

index.html

css

index.css

js

index.js

copy
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1>Shopping Cart</h1>
<input
type="text"
id="product-name-input"
placeholder="Enter product name"
/>
<button id="add-to-cart-btn">Add to Cart</button>
<ul id="cart-list"></ul>
<script src="index.js"></script>
</body>
</html>
  • Use createElement to create a new <li> element;

  • Use createElement to create a new <button> element;

  • Use appendChild to add new <button> element to the <li> element;

  • Use appendChild to add a new <li> element to the <ul> with the ID cart-list;

  • Use removeChild to delete the specific <li> element from the <ul>.

html

index.html

css

index.css

js

index.js

copy
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1>Shopping Cart</h1>
<input
type="text"
id="product-name-input"
placeholder="Enter product name"
/>
<button id="add-to-cart-btn">Add to Cart</button>
<ul id="cart-list"></ul>
<script src="index.js"></script>
</body>
</html>

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 11

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt