jquery - Splice removing random elements issue in javascript -


i trying create interactive shopping cart using jquery, splice issue ruined everything, because when delete element directly works perfect me, going dynamically changes doesn't work deleting random elements in array after updates indexes.

<script src="//code.jquery.com/jquery-1.10.2.js"></script> <div id="mydiv2"></div> <div id="mydiv"></div> <script>  var wishlistarray = ["item1", "item2", "item3", "item4", "item5"];   function wishlist_show() {       var wishlistarraydom = "";      jquery.each(wishlistarray, function (i, wishlistid) {            wishlistarraydom += '<div><a href="javascript:;" class="rem-from-wishlist" value="' + + '">remove( ' + wishlistid + ' )</a></div>';      });       $('#mydiv').empty();      $('#mydiv').html(wishlistarraydom);      wishlist_update_rem();       console.log('all: ' + wishlistarray.length);      console.log('arr: ' + wishlistarray);  }   function wishlist_rem(num) {      console.log('value: ' + $(this).attr('value'));      console.log('all: ' + wishlistarray.length);      console.log('arr: ' + wishlistarray);      $(this).parent().slideup("slow", function () {          console.log(wishlistarray.splice($(this).attr('value'), 1));          wishlist_show();       });      $('#mydiv2').append('<br>remove ' + wishlistarray[parseint($(this).attr('value'))] + ' of element' + +$(this).attr('value'));  }   wishlist_show();   function wishlist_update_rem() {      $(".rem-from-wishlist").unbind("click");      $(".rem-from-wishlist").click(wishlist_rem);  } </script> 

jsfiddle example

update: solved myself, all, stackoverflow best.

ready used jsfiddle http://jsfiddle.net/ar0zrxwc/

it's not issue splice(), it's issue scoping of this.

change removal function this:

function wishlist_rem(num) {       var idx = parseint($(this).attr('value')));      $(this).parent().slideup("slow", function () {          wishlistarray.splice(idx, 1));          wishlist_show();       });      $('#mydiv2').append('<br>remove ' + wishlistarray[parseint($(this).attr('value'))] + ' of element' + +$(this).attr('value'));  } 

Comments

Popular posts from this blog

javascript - IndexedDB error: Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned -

amazon web services - When using Cognito credentials with AWS in a browser (javascript), keep getting "missing credentials" error -

c# - How to set the datatextfield value instead of the value on a drop down list? -