I have a list of items to put in a box and i have an empty box, the list of items and the empty box are two different arrays, the items are objects in the array put by user inputs. Beside every item added is an "add" button appended to it which should then copy that selected object into the empty box array. How do i achieve this?
i have tried to append the parentNode of the add button and push to the new array but it just pushed the "Li" element instead of the object itself
<div>
<input id="userinput" type="number" placeholder="Enter Capacity">
<button id="enter">Enter</button>
</div><br>
<div>
<p>Items Information:</p>
<input id="itemName" type="text" placeholder="enter item name">
<input id="itemWeight" type="number" placeholder="enter item weight(kg)">
<input id="itemValue" type="number" placeholder="enter item value">
<button onclick="addListAfterClick()" id="value">Enter</button>
</div>
<ul id="list">LIST OF 20 ITEMS TO CHOOSE FROM
</ul>
<ul id="knap"> LIST OF ITEMS IN KNAPSACK
</ul>
<div>
let addValues = () =>{
inputs = {
name : input2.value,
weight : parseFloat(input3.value),
value : parseInt(input4.value)
}
arr_items.push(inputs);
console.log(arr_items);
createListElement();
}
let createListElement = () => {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input2.value));
ul.appendChild(li);
let btn = document.createElement("button");
btn.appendChild(document.createTextNode("Add"));
li.appendChild(btn);
btn.onclick = addTo;
}
function addTo(){
var li2 = document.createElement("li");
li2.appendChild(document.createTextNode(input2.value));
ul2.appendChild(li2);
knap.push(this.parentNode);
console.log(knap);
}
I expect any object clicked to be copied to the "knap" array