scrimba
Learn JavaScript
Build a Mobile App
Removing an item when clicked
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Removing an item when clicked
AboutCommentsNotes
Removing an item when clicked
Expand for more info
index.js
run
preview
console
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js"
import { getDatabase, ref, push, onValue } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js"

const appSettings = {
databaseURL: "https://realtime-database-df319-default-rtdb.europe-west1.firebasedatabase.app/"
}

const app = initializeApp(appSettings)
const database = getDatabase(app)
const shoppingListInDB = ref(database, "shoppingList")

const inputFieldEl = document.getElementById("input-field")
const addButtonEl = document.getElementById("add-button")
const shoppingListEl = document.getElementById("shopping-list")

addButtonEl.addEventListener("click", function() {
let inputValue = inputFieldEl.value

push(shoppingListInDB, inputValue)

clearInputFieldEl()
})

onValue(shoppingListInDB, function(snapshot) {
let itemsArray = Object.entries(snapshot.val())

clearShoppingListEl()

for (let i = 0; i < itemsArray.length; i++) {
let currentItem = itemsArray[i]
let currentItemID = currentItem[0]
let currentItemValue = currentItem[1]

appendItemToShoppingListEl(currentItem)
}
})

function clearShoppingListEl() {
shoppingListEl.innerHTML = ""
}

function clearInputFieldEl() {
inputFieldEl.value = ""
}

function appendItemToShoppingListEl(item) {
let itemID = item[0]
let itemValue = item[1]

let newEl = document.createElement("li")

newEl.textContent = itemValue

shoppingListEl.append(newEl)
}
Console
/index.html
-4:10