1

I'm using the dragula library to create a drag and drop interaction.

See fiddle here.

Each time an item is dropped, the following code fires:

dragula([$("#left").get(0)])
.on("dragend", function(el, target, src) {
items = []; // reset items
$(".item").each(function(idx, item) {
items.push($(item).text());
});

This gets the text from the items and adds it to an array.

All good. This part works great.

Now, if I change the value of the variables in the array, how can I push this change back to the dragula interaction so the order of the items matches the array?

Thanks!

2
  • Access item from array using index and manipulate it..You are not suppose to extract and push it back.. Commented May 30, 2016 at 4:11
  • Thanks Rayon, I appreciate your help. Commented May 30, 2016 at 6:45

1 Answer 1

1

I just refered your code and edited this function such that you can get the source of each div that is being dragged. You shouldn't extract each element to edit them and then push it again.. instead you shall get that element through index or as the function below. That will keep things simple.

dragula([$("#left").get(0)])
.on("dragend", function(el, target, src) {
    items = []; // reset items
  var itemname = el.innerHTML;
  el.innerHTML = itemname+"<span>New Item </span>";
    console.log(el);
//  $(".item").each(function(idx, item) {
  //    items.push($(item).text());
  //});

  //console.log(items);

});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much for helping Tirthraj. I think I might be over complicating things. After the drag and drop adds the values to the array, they are then split into separate variables and passed to the parent window. What I then need to do is get the updated variables value back from the parent window into the appropriate divs. Does that make sense?
Yes it does but still don't take the element out of the array and push it back.. You shall do the whole operation on the same element without disturbing its index in array.. This will not only simplify your code but also allow you to do something else to the element.. I mean styling or anything else if you need to.
Okay, excellent. My lack of JavaScript knowledge is making this a lot harder than it needs to be. Thanks for your patience. I'll spend some time later trying to implement this and will comment back here if (or should I say when!) I run into issues. Once again, I really appreciate your help.
Nope.. I would be happy if you mark my answer as correct if you got proper help from me.
Of course Tirthraj, I'll come back and mark your answer as correct after I've had some time to implement it. Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.