jquery - .splice( $.inArray()) should only remove matching item -


i have 5 links. each link has id. save each of these id's array.

when clicking on link im trying remove matching clicked link id within array.

im trying following:

shuffledblockids.splice( $.inarray(removeitem, shuffledblockids), 1 ); 

the item removes fine first click, if click again remove yet item (although clicked id no longer exists).

how remove item, if clicked id exists in array?

had @ indexof() should supposedly not work in ie8.

ie9+ solution welcomed - wondering if there's smart jquery approach taking care of ie8.

fiddle: http://jsfiddle.net/dyoew9ga/

if removeitem not present in shuffledblockids, $.inarray() return -1.

the documentation splice() says (emphasis mine):

start

index @ start changing array. if greater length of array, actual starting index set length of array. if negative, begin many elements end.

therefore, splice() end removing last element array.

to work around this, use explicit test:

var index = $.inarray(removeitem, shuffledblockids); if (index >= 0) {     shuffledblockids.splice(index, 1); } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -