-2

Hi I have tried using "indexOf" but still it seems like i remove the last element of the array and not the chosen element...

How I can fix this, so it is the element I choose that is being the removed and not the last or first element...

I HAVE TRIED USING "indexOf" I HAVE TRIED.... I KNOW HOW TO REMOVE AND ELEMENT BUT NOW THE ELEMENT THE POSITION I DONT KNOW HOW TO REMOVE???

function getTodoItems() {
    for (var i = 0; i < dataArray.length; i++) {
        if (!dataArray[i].listItem.length) return;
        var itemList = document.getElementById("my-todo-list");
        var list = document.createElement("li");
        itemList.appendChild(list);
        list.innerHTML = dataArray[i].listItem;
        var spanItem = document.createElement('span');
        spanItem.style.float = 'right';
        var myCloseSymbol = document.createTextNode('\u00D7');
        spanItem.classList.add("closeBtn");
        spanItem.appendChild(myCloseSymbol);
        listItems[i].appendChild(spanItem);

///////////////////////////// Relevant Section /////////////////////////////

        close[i].onclick = function() {
            var div = this.parentElement;
            div.style.display = "none";
            var position = dataArray.indexOf(dataArray[i]); // Fix denne her linje
            dataArray.splice(position, 1); // Fix denne her linje
            localStorage.setItem("itemListRecord", JSON.stringify(dataArray));
            console.log(dataArray);
        }

/////////////////////////////////////////////////////////////////////////////

        var list = document.getElementsByTagName('li');
        list[i].onclick = function() {
            this.classList.toggle("checked");
        }

    }
}

https://jsfiddle.net/headbangz/r2sx0yhz/

Here is how my code is looking

2 Answers2

0

You can use Array.filter in order to filter our unwanted elements.

const arr = [1, 2, 3, 4, 5];

const result = arr.filter((item) => item % 2 === 0);

console.log(result);

Your problem in the code probably related to the fact that when you set the onclick the closure is points to the i value and not incapsulate it like you probably wanted.

You can use IIFE in-order to save the i value.

close[i].onclick = (function(i){
       return function() {
            var div = this.parentElement;
            div.style.display = "none";
            var position = dataArray.indexOf(dataArray[i]); // Fix denne her linje
            dataArray.splice(position, 1); // Fix denne her linje
            localStorage.setItem("itemListRecord", JSON.stringify(dataArray));
            console.log(dataArray);
        }
}(i))
felixmosh
  • 20,379
  • 4
  • 36
  • 56
0

This is the issue: var position = dataArray.indexOf(dataArray[i]);

When your loop is done running, i will always refer to the index of the last item. As an example, this function looks like it might print 1, 2, 3, 4, 5. In reality, it prints 5 five times instead. By the time the console.log part actually runs, the var i is already equal to 5.:

for (var i = 0; i < 5; ++i){ setTimeout(function(){ console.log(i); }, 2000) }

^ Try pasting this into your console.

Anyway, to fix it, you need to find the index of the item in your array, where the key:value pair matches the text of the todo item you need to remove.

close[i].onclick = function(e) {
            var div = e.target.parentElement;
            var divText = div.childNodes[0].nodeValue; // use this!
            div.style.display = "none";
            var position = dataArray.findIndex(todo => todo.listItem === divText);
            dataArray.splice(position, 1);
            localStorage.setItem("itemListRecord", JSON.stringify(dataArray));
        }

Working Fiddle: https://jsfiddle.net/r2sx0yhz/3/

SideNote: This should also be changed. Try your fiddle in an incognito window to see why.

// this is bad... a user may not have anything in their localStorage yet.
var dataArray = [];
dataArray = JSON.parse(localStorage.getItem("itemListRecord"));
// do this instead.
var dataArray = JSON.parse(localStorage.getItem("itemListRecord")) || new Array();
TJBlackman
  • 1,216
  • 1
  • 12
  • 31