0

If I have a form with 5 children elements, is there a way to swap, for example, the second one and the 4th one via that pseudo array children of the form? Like:

let temp = parent_form.children[1st_swapped_elem_index];
parent_form.children[1st_swapped_elem_index] =       parent_form.children[2nd_swapped_elem_index];
parent_form.children[1st_swapped_elem_index] = temp;

But it doesnt work, nothing happens. As the 1st_swapped_elem_index and 2nd_swapped_elem_index there are legal, usable and defined variables, each of them holds a certain DOM element.

7
  • What is the actual problem your trying to solve? The way items are displayed in a list to the user? The iteration order using forEach/for loop? Commented Apr 7, 2016 at 15:55
  • 1
    The problem is - there are few input intems, I want to make simple todo list with HTML5 drag and drop API, and when I drop the dragged item on some other, they are supposed to switch their positions on the screen. So I am trying to use that piece of code to change their positions in the children array-ish and I'm hoping a browser would render them on the new places. But they dont switch places in the children. The console doesn't log any errors though, the temp value gets the 1st_swapped_elem, but it doesn't swap them in the html collection
    – Alex Buddy
    Commented Apr 7, 2016 at 16:03
  • No there is no loop, I just wrote the items dowm in index.html
    – Alex Buddy
    Commented Apr 7, 2016 at 16:05
  • I couldn't do it via append child or insertBefore
    – Alex Buddy
    Commented Apr 7, 2016 at 16:07

0

Your Answer

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

Browse other questions tagged or ask your own question.