Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.  
  6. <script>
  7.  
  8. function sortList() {
  9.  
  10. var list = document.getElementById('list').childNodes;
  11. var array = [];
  12. for(var i=0;i < list.length; i++) {
  13. var value = list[i].innerHTML;
  14. if (typeof value != 'undefined') array.push(value);
  15. }
  16.  
  17. array.sort();
  18.  
  19. document.getElementById('list').innerHTML = '';
  20.  
  21. for(var i=0;i < array.length; i++) {
  22. var child = document.createElement('li');
  23. child.innerHTML = array[i];
  24. document.getElementById('list').appendChild(child);
  25. }
  26.  
  27. }
  28.  
  29. </script>
  30.  
  31. </head>
  32.  
  33. <body>
  34.  
  35. <ol id="list">
  36. <li>peach</li>
  37. <li>apple</li>
  38. <li>orange</li>
  39. <li>grapes</li>
  40. <li>apricot</li>
  41. <li>banana</li>
  42. </ol>
  43.  
  44. <button type="button" onclick="sortList();">Sort Me!</button>
  45.  
  46. </body>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement