I have a div that then contains a list of child divs that represent a list member. I am trying to create a set of buttons to move an list item up or down in the list. I can retrieve the index of the list, but I don't know how to change the positions of the current elements
function moveUp(){
let list = document.getElementById("answer").children;
let targeted = document.getElementById("answer").getElementsByClassName("active")
var temp = Array.prototype.indexOf.call(list,targeted[0])
console.log(temp)
if(temp > 0){
list[temp] = list[temp-1];
list[temp-1] = targeted[0]
}
}
function moveDown(){
let list = document.getElementById("answer").children;
var temp = Array.prototype.indexOf.call(list,document.getElementById("answer").getElementsByClassName("active"))
if(temp < list.length-1){
list.swap(temp, temp+1);
}
}
<div class="white" id="answer" name="answer" style="height:21.125vh; width:35vw; box-sizing: border-box; overflow: auto;">
<div id="1" class="" style="height: 5vh; font-size: 2vw; color: black; text-align: center; justify-content: center; overflow: auto;">test</div>
<div id="2" class="active" style="height: 5vh; font-size: 2vw; color: black; text-align: center; justify-content: center; overflow: auto;">commands</div>
<div id="21" class="" style="height: 5vh; font-size: 2vw; color: black; text-align: center; justify-content: center; overflow: auto;">Final</div>
</div>
That is the test list of divs I am trying to move. The test I attempt is selecting the div with the innerhtml "commands" and moving it up and nothing happens. Pressing the down button get the error
Uncaught TypeError: list.swap is not a function
If I could get some help with this, that would be great.