Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

sort li elements with js

const ul = document.querySelector('ul');
const liElements = Array.from(document.querySelectorAll('li')); //creating array from the li elements

//Sorting the li elements by text content Asc order
liElements.sort((li1,li2) => li1.textContent.localeCompare(li2.textContent));

//Append the sorted li elemtns into the ul 
appendCh(liElements,ul)

//Function that will append them all
function appendCh(liArr,parent){
  
        liArr.forEach(el => {
            parent.appendChild(el);
        })
    }
 
PREVIOUS NEXT
Tagged: #sort #li #elements #js
ADD COMMENT
Topic
Name
4+3 =