Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function sortList() {
- var list = document.getElementById('list').childNodes;
- var array = [];
- for(var i=0;i < list.length; i++) {
- var value = list[i].innerHTML;
- if (typeof value != 'undefined') array.push(value);
- }
- array.sort();
- document.getElementById('list').innerHTML = '';
- for(var i=0;i < array.length; i++) {
- var child = document.createElement('li');
- child.innerHTML = array[i];
- document.getElementById('list').appendChild(child);
- }
- }
- </script>
- </head>
- <body>
- <ol id="list">
- <li>peach</li>
- <li>apple</li>
- <li>orange</li>
- <li>grapes</li>
- <li>apricot</li>
- <li>banana</li>
- </ol>
- <button type="button" onclick="sortList();">Sort Me!</button>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement