I’m currently making a website with a "shopping list" functionality. I can create an array and add items to the "shopping list" via the input field, and I can also remove the latest input using a button.
As of now, the list shows all new items in order of input. But, when I remove the latest item, it shows only that item. And then when I add a new item, the full, updated list shows.
These pictures show how it currently looks:
- Add items, that show up in the list ¨
- Remove item – the list disappears and only the latest item being removed is shown
- The new, updated list shows ONLY after I’ve added a new item to the list ("four")
What I’m trying to do is this:
- Add items – they show up in the list, as now
- Remove item – the latest item is just removed from the list, but the list still shows previous items remaining. The picture here is how I want it to look.
var arrayOfWord = [];
var inputElement = document.getElementById('userinput');
var errorElement = document.getElementById('error');
var wordsElement = document.getElementById('words');
function addWord() {
errorElement.innerHTML = "";
var word = inputElement.value;
if (word.trim() === "")
errorElement.innerHTML = "Empty input, write an item you want to put on the list!";
else
arrayOfWord.push(word);
inputElement.value = "";
process();
}
function process() {
words.innerHTML = arrayOfWord.join(' <br><br> ');
}
function remove() {
words.innerHTML = arrayOfWord.pop();
document.getElementById('words').innerHTML = parsed;
}
<p>
<ol id="demo"></ol>
</p>
<input id="userinput" /><button onclick="addWord()">Add item</button>
<div id="error"></div>
<button onclick="remove()">Remove</button><br>
<p id="testing"></p>
<p id="words"></p><br>
<a href="#" class="action">Buy Now</a>
</div>
3
Answers
The issue is because of this line in
remove()
:That is removing the last item from the array, but it then sets that item, which has been removed from your array, as the visible text within the element.
To fix the problem call
pop()
without setting the HTML of the element directly, then call yourprocess()
function to update the DOM based on thenew content of the array:
Here’s a full working example:
Also, as a side note, be aware that
ol
elements are not valid withinp
(further reading on this here), and it’s much better practice to bind your event handlers unobtrusively in JS usingaddEventListener()
instead of usingonX
attributes in your HTML.The problem you are facing is most probably because of arrayOfWord.pop() method in function remove. You are assigning the value of arrayOfWord.pop() to words.innerHTML , which eventually returns the deleted element not the the whole array.
To fix this issue you can think of another approach like re rendering the whole array as updated result
Use pop method but don’t render it to your page . Hope this will work for you
It is generally not a good idea to use inline event handlers. Furthermore, I’d say the interface is not so clear. How would a user know what the
remove
button does? Wouldn’t it be an idea to enable removing one of all items instead of the last added?With that in mind I suggest something like this (using event delegation and a factory function for the handling):