Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active June 14, 2023 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradmontgomery/658d16e6359cba593c05d31dd9d5b249 to your computer and use it in GitHub Desktop.
Save bradmontgomery/658d16e6359cba593c05d31dd9d5b249 to your computer and use it in GitHub Desktop.
Forms & DOM nodes
<h1>Grocery List</h1>
<form action="" method="get" id="groceries">
<label for="item">Grocery Item</label>
<input type="text" id="item" name="item">
<input type="submit" value="Add Item">
</form>
<ul id="grocery-items">
</ul>
/*
* This code is for our Grocery List
*/
const groceryForm = document.getElementById("groceries");
groceryForm.addEventListener("submit", (event) => {
event.preventDefault();
// get user data from form
const item = document.getElementById("item").value;
// Create a <li> DOM node with the item, and
// insert into the page.
const li = document.createElement("li");
li.innerHTML = item;
const parent = document.getElementById("grocery-items");
parent.appendChild(li);
});
// TODO: Click on the <li> elements, to "cross them out"
// either use <s> or the css text-decoration: line-through;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment