I have an list of values with up and down button. If i want to click up button the element with move upwards with previous value in list and i click down button they moves downwards to next item in list. my sample code is here,
<ul>
<li> 1 <button class="up">UP</button> <button class="down">DOWN</button></li>
<li> 2 <button class="up">UP</button> <button class="down">DOWN</button></li>
<li> 3 <button class="up">UP</button> <button class="down">DOWN</button></li>
<li> 4 <button class="up">UP</button> <button class="down">DOWN</button></li>
<li> 5 <button class="up">UP</button> <button class="down">DOWN</button></li>
</ul>
<script type="text/javascript">
function moveUp(element) {
if(element.previousElementSibling)
element.parentNode.insertBefore(element, element.previousElementSibling);
}
function moveDown(element) {
if(element.nextElementSibling)
element.parentNode.insertBefore(element.nextElementSibling, element);
}
document.querySelector('ul').addEventListener('click', function(e) {
if(e.target.className === 'down') moveDown(e.target.parentNode);
else if(e.target.className === 'up') moveUp(e.target.parentNode);
});
</script>
This is the code with list of values to display, but i want array values to display in this format which performs up and down function based on index. my array element is:
[
{ id: "Racer-101", rank: "1"},
{ id: "Racer-102", rank: "2"},
{ id: "Racer-103", rank: "3"},
{ id: "Racer-104", rank: "4"},
{ id: "Racer-105", rank: "5"},
{ id: "Racer-106", rank: "6"},
{ id: "Racer-107", rank: "7"},
{ id: "Racer-108", rank: "8"},
{ id: "Racer-109", rank: "9"}
]
how is it possible with array values..
moveUp()andmoveDown()functions with buttons in your code.