move list items up and down using javascript

Solutions on MaxInterview for move list items up and down using javascript by the best coders in the world

showing results for - "move list items up and down using javascript"
Safia
09 Aug 2020
1window.onload = function () {
2	var upLink = document.querySelectorAll(".up");
3
4	for (var i = 0; i < upLink.length; i++) {
5		upLink[i].addEventListener('click', function () {
6			var wrapper = this.parentElement;
7
8			if (wrapper.previousElementSibling)
9			    wrapper.parentNode.insertBefore(wrapper, wrapper.previousElementSibling);
10		});
11	}
12
13	var downLink = document.querySelectorAll(".down");
14
15	for (var i = 0; i < downLink.length; i++) {
16		downLink[i].addEventListener('click', function () {
17			var wrapper = this.parentElement;
18
19			if (wrapper.nextElementSibling)
20			    wrapper.parentNode.insertBefore(wrapper.nextElementSibling, wrapper);
21		});
22	}
23}