What i have: small script which appends list elements when the user clicks on a button with .
What i want: The user can dynamically type input and the input will be used as a text for the list element
What doesn't work: It does not save the value from the input, it always shows empty. I suspect it has something to do with the data not being loaded and still using the default, but i am not sure.. Started with js a couple of days ago, so i am fairly new with the DOM concepts.
var userInput = document.getElementById("userChoice").value;
var textNodeValue = userInput;
// remove a list element when the user clicks on remove item
function deleteElement(clicked_id) {
//save list element
var clickedButton = document.getElementById(clicked_id);
console.log(clickedButton);
var parentNodeLi = clickedButton.parentElement;
var listElement = parentNodeLi.parentElement;
listElement.removeChild(parentNodeLi);
}
// adds a new list item
function addListItem(textNodeValue) {
//add list item, button and image to button
var newElem = document.createElement("li");
var newButton = document.createElement("button");
var newImage = document.createElement("img");
//add text nodes
var newNode = document.createTextNode(textNodeValue);
var newNodeButton = document.createTextNode("");
newImage.src = "button_delete.jpg"; //add src for image
newImage.height = 15;
//add id to the button
var nodeList = document.getElementsByTagName("li");
var idIncrementor = nodeList.length + 1;
console.log(idIncrementor);
newButton.setAttribute("id", "button".concat(idIncrementor));
console.log(newButton.getAttribute("id"));
//add anonymous function to the evenListerner to call the deleteElement function
//use it for parameter to the onclick function
newButton.addEventListener('click', function() {
deleteElement(newButton.getAttribute("id"))
});
// add the image for the button
newButton.appendChild(newImage);
//append text node and button to the new list element
newElem.appendChild(newNode);
newElem.appendChild(newButton);
//get the unorderedList and append the new list
var unorderedList = document.getElementById("list");
unorderedList.appendChild(newElem);
}
<div id="newItemContainer">
<input id="userChoice">
<button onclick="addListItem(textNodeValue)">Add item </button>
</div>
<div id="shoppingListContainer">
<ul id="list">
<li>List 1 <button class="listItems" id="button1" onclick="deleteElement(this.id)"><img src="button_delete.jpg" height=15/> </button> </li>
<li>List 2 <button class="listItems" id="button2" onclick="deleteElement(this.id)"><img src="button_delete.jpg" height=15/> </button></li>
<li>List 3 <button class="listItems" id="button3" onclick="deleteElement(this.id)"><img src="button_delete.jpg" height=15/> </button></li>
<li>List 4 <button class="listItems" id="button4" onclick="deleteElement(this.id)"><img src="button_delete.jpg" height=15/></button></li>
</ul>
</div>