Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. <script>
  2. /*
  3. * This simple script will take multiple html elements, like a group of <li> or <span> or anything,
  4. * and reorder them by the data attribute.
  5. * Example:
  6. * The script will reorder this:
  7. * <span data-test="1">Number 1</span>
  8. * <span data-test="4">Number 2</span>
  9. * <span data-test="3">Number 3</span>
  10. * <span data-test="2">Number 4</span>
  11. *
  12. * into this:
  13. * <span data-test="1">Number 1</span>
  14. * <span data-test="2">Number 4</span>
  15. * <span data-test="3">Number 3</span>
  16. * <span data-test="4">Number 2</span>
  17. *
  18. * add the target withing the querySelectorAll('CSS SELECTOR GOES HERE')
  19. */
  20.  
  21. Array.prototype.slice.call(document.querySelectorAll('.column-1 span[data-test]')).sort(function(a, b) {
  22.  
  23. // Add the data attribute in both of these as well
  24.  
  25. a = a.getAttribute('data-authorOrder');
  26. b = b.getAttribute('data-authorOrder');
  27. return a.localeCompare(b);
  28. }).forEach(function(a) {
  29. a.parentNode.appendChild(a);
  30. });
  31.  
  32. </script>
Add Comment
Please, Sign In to add comment