javascript - Find duplicate item in unordered list -
javascript - Find duplicate item in unordered list -
i have 2 unordered lists. 1 unordered lists populated dynamically , double click it's items add together other unordered list.
i'm trying figure out how observe if item on dynamically populated list isn't in other list. if is. shouldn't added again. don't want add together duplicate items.
the code populates ul:
.done(function(html) { var results = jquery.parsejson(html); $('#store_sel').children().remove(); (var = 0; <= results.length; i++){ $("#store_selection").append("<li class='" + results[i].name + " stores'" + "id= " + results[i].uuid + + ">" + results[i].name + "</li>"); } });
the event:
$('#store_selection').on('dblclick', 'li', function(e) { e.stoppropagation(); e.preventdefault(); if ($('#store_selected').find($(this))) { console.log('item there'); } else { $('#store_selected').append($(this).clone()); //$(this).remove(); } });
edit: why isn't working? goes console.log if ul empty.
there several issues within if
statement.
this
in 2 lists an object homecoming truthy. utilize length
of returned selector collection instead since id's must unique in page suggest utilize data-
attribute store uuid
<li data-uuid="xxxx-yyy">
then when search:
var uuid = $(this).data('uuid') if ($('#store_selected').find('.stores[data-uuid='+uuid+']').length) { console.log('item there'); } else { $('#store_selected').append($(this).clone()); //$(this).remove(); }
javascript jquery ajax
Comments
Post a Comment