scrimba
Note at 1:44
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

Note at 1:44
AboutCommentsNotes
Note at 1:44
Expand for more info
index.js
run
preview
console
/** Challenge: 
- Iterate over the wishlist array.
- Dynamically render your wishlist from the array.
- Style the wishlist with CSS.
*/

let wishListHtml = document.getElementById("wishlist")
let btn = document.getElementById("button")
btn.addEventListener("click", onButtonClick)

const wishlist = [
// TODO: Add more items here
"A dog",
"A pet raccoon",
"A pet opossum"
]

function getWishList(list) {
list.forEach(item => {
console.log(item)
// creating a new list item element
const listItem = document.createElement("li")
listItem.textContent = item
wishListHtml.appendChild(listItem)
addRemoveBtn(listItem);
})
return wishListHtml
}

getWishList(wishlist)


function onButtonClick() {
const myTextValue = document.getElementById("myText").value

//with .trim(), spaces are ignored and we're checking for the actual content of the input
//ex:___hello will be accepted, "" is not
if (myTextValue.trim() === "") {
alert("Please enter a value before adding to the wishlist.");
return
}

const listItem = document.createElement("li")
listItem.textContent = myTextValue
wishListHtml.appendChild(listItem)
console.log(myTextValue)
addRemoveBtn(listItem)
clearField()
}

function clearField() {
document.getElementById("myText").value = ""
}

function addRemoveBtn(listItem) {
const removeBtn = document.createElement("button")
removeBtn.setAttribute("class", "remove-button")
removeBtn.innerHTML = "Delete"
removeBtn.addEventListener("click", function() {
wishListHtml.removeChild(listItem)
removeBtn.parentNode.removeChild(removeBtn)
})
listItem.appendChild(removeBtn)
}

Console
"A dog"
,
"A pet raccoon"
,
"A pet opossum"
,
"cookies"
,
" trees"
,
"elephant"
,
/index.html
LIVE