Patiphan Mahamat
Patiphan Mahamat

Reputation: 1

how to delete specific value from array jquery

Hello I can't find out how to delete a specific value from string when clicking on an element with the string value. its for my todo list.

if (window.localStorage.fullName !== undefined) {
alert('Your browser does not support localStorage');
} else {
$(function () {
    console.log('localSorage compitability detected. proceeding...');

global vars & functions

    var tdli = [];

add list items

    $('#tdladd').click(function () {
        $('.todolist ul ul').append("<li class='tdli'>" + $('#tdlinput').val() + "</li>");
        tdli.push($('#tdlinput').val());
        $('#tdlinput').val('');
        var array = tdli + "";
        localStorage.setItem('tdlis', array);
        console.log(array);
        $('#todolist').hide().fadeIn('fast');
    });

remove list items

    $('li').click(function () {
        var itemtoRemove = $(this);
        tdli.splice($.inArray(itemtoRemove, tdli), 1);
        console.log(tdli);
    });

    $('#clearall').click(function () {
        localStorage.clear();
        location.reload();
    });

load list items

    var tdlitems = localStorage.getItem('tdlis');

    var array = tdlitems.split(',');

    tdli.push(array);

    for (var i = 0; i < array.length; i++) {
        array[i] + "<br>";
        $('.todolist ul ul').append("<li>" + array[i] + "</li>");
    };

    console.log(array);


});

}

Upvotes: 0

Views: 77

Answers (1)

jAndy
jAndy

Reputation: 235972

Assuming that tdli is a jQuery wrapped set (which itself is an array-like-object), it will have DOM nodes stored instead of another jQuery objects. That means, just go like

var itemtoRemove = this;

and you should be fine.


After you posted your complete code, we can see you're actually pushing string values into your tdli Array, but you try to .splice() objects respectively DOM nodes, which of course doesn't make any sense at all (comparing apples to oranges).

Upvotes: 2

Related Questions