2

I am doing some html5 / jquery drag and drop functionalities to reorder a set of DOM elements. I now want to change an array of objects that correspond to these DOM elements, but I'm not quite sure how to do it. Here's the javascript:

var draggedIndex = $('.segmentListItem').index($(draggedItem)); var targetIndex = $('.segmentListItem').index($(this)); var playlist = jwplayer().getPlaylist(); //MH - the array for which I want to change the order if (draggedIndex > targetIndex){ $(draggedItem).insertBefore($(this)); //MH - need to move the playlist item at the index of the dragged item before index the target item as well } else { $(draggedItem).insertAfter($(this)); //MH - need to move the playlist item at the index of the dragged item before index the target item as well } 

1 Answer 1

2

If playlist is a regular array, not an object (you are reffering to it as an array), maybe something like this:

Array.prototype.move = function (old_index, new_index) { if (new_index >= this.length) { var k = new_index - this.length; while ((k--) + 1) { this.push(undefined); } } this.splice(new_index, 0, this.splice(old_index, 1)[0]); }; var draggedIndex = $('.segmentListItem').index($(draggedItem)); var targetIndex = $('.segmentListItem').index($(this)); var playlist = jwplayer().getPlaylist(); //MH - the array for which I want to change the order if (draggedIndex > targetIndex){ $(draggedItem).insertBefore($(this)); playlist.move(draggedIndex, targetIndex); } else { $(draggedItem).insertAfter($(this)); playlist.move(draggedIndex, targetIndex); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.