Not jQuery! I just want to move the clicked list item to the top of the list with vanilla JavaScript. I found jQuery solutions but couldn't figure it out in vanilla JavaScript. I also don't want to use any class or id for the list and list items that's why my JS looks a bit messy I think.
Example: If I click the second item (2), the new order will be 2, 1, 3.
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
My try based on the link https://css-tricks.com/snippets/jquery/move-clicked-list-items-to-top-of-list/
const ulElement = document.getElementsByTagName('ul')[0];
const liElements = document.getElementsByTagName('ul')[0].childNodes;
for (let i = 0; i < liElements.length; i++) {
liElements[i].addEventListener('click', () => {
ulElement.insertBefore(this, ulElement.childNodes[0]);
});
}