chrome like it but IE does not, somearray ends up with some deleted object data


SUBMITTED BY: Guest

DATE: Sept. 23, 2015, 4:04 a.m.

FORMAT: Text only

SIZE: 5.2 kB

HITS: 1013

  1. <!-- Call this function each time -->
  2. sortHTMLList('itemname','innerHTML','no');
  3. //This function grabs all of the HTML elements with the same class name,
  4. //then sorts all of those items alphabetically ascending based on sortField.
  5. //sortField can be innerHTML or outerHTML (with fieldIsNode no) or sortField can
  6. //also be fields from nodes within the items within the HTMLCollection (using
  7. //fieldIsNode = 'yes')
  8. //- This variable will hold an HTMLCollection
  9. var someElement = {};
  10. var someArray = [];
  11. var i;
  12. function sortHTMLList(sortCollection, sortField, fieldIsNode){
  13. try {
  14. //- Grab all HTML elements with the specified class name
  15. someElement = document.getElementsByClassName(sortCollection);
  16. //-Copy the document into an array, because sort functions only work on
  17. //-arrays, not objects
  18. someArray = [].slice.call(someElement);
  19. //-The rest of the following is code related to sorting. this sort code
  20. //-works well to sort a mix of letters and numbers with mixed capitalization
  21. var reA = /[^a-zA-Z]/g;
  22. var reN = /[^0-9]/g;
  23. //-We have to change the way we ask for the fields if we are trying to read/
  24. //-the value out of child node of the item in the HTMLCollection
  25. if(fieldIsNode === 'yes')
  26. {
  27. someArray.sort(function(a,b) {
  28. //-If an object yields a null value, javascript execution halts. Don't process nulls.
  29. if((a.attributes[sortField].nodeValue !== null) && (b.attributes[sortField].nodeValue !== null))
  30. {
  31. var smlla = a.attributes[sortField].nodeValue.toLowerCase();
  32. var smllb = b.attributes[sortField].nodeValue.toLowerCase();
  33. var aA = smlla.replace(reA, "");
  34. var bA = smllb.replace(reA, "");
  35. if(aA === bA) {
  36. var aN = parseInt(a.attributes[sortField].nodeValue.replace(reN, ""), 10);
  37. var bN = parseInt(b.attributes[sortField].nodeValue.replace(reN, ""), 10);
  38. return aN === bN ? 0 : aN > bN ? 1 : -1;
  39. } else {
  40. return aA > bA ? 1 : -1;
  41. }
  42. }
  43. else
  44. return 0;
  45. });
  46. }
  47. //Else, we just want values off the master node of the item of the HTMLCollection
  48. else{
  49. someArray.sort(function(a,b) {
  50. //-If an object yields a null value, javascript execution halts. Don't process nulls.
  51. if((a[sortField] !== null) && (b[sortField] !== null))
  52. {
  53. var smlla = a[sortField].toLowerCase();
  54. var smllb = b[sortField].toLowerCase();
  55. var aA = smlla.replace(reA, "");
  56. var bA = smllb.replace(reA, "");
  57. if(aA === bA) {
  58. var aN = parseInt(a[sortField].replace(reN, ""), 10);
  59. var bN = parseInt(b[sortField].replace(reN, ""), 10);
  60. return aN === bN ? 0 : aN > bN ? 1 : -1;
  61. } else {
  62. return aA > bA ? 1 : -1;
  63. }
  64. }
  65. else
  66. return 0;
  67. });
  68. }
  69. //We assume that someArray is the same size as someElement.
  70. //We iterate each through item and replace the someElement outerHTML
  71. //with all the new sorted outerHTML's from someArray. The element
  72. //is "live", so the document gets updated as the element is updated.
  73. var mylength = someArray.length;
  74. for (var i = 0, len = mylength; i < len; i++ ) {
  75. //console.log("before");
  76. //console.log(someElement[2].outerHTML);
  77. //console.log(someArray[2].outerHTML);
  78. console.log(someArray.length);
  79. console.log(someElement.length);
  80. console.log(i);
  81. //var temp = someElement[i].outerHTML;
  82. //someElement[i].outerHTML = 5;
  83. someElement[i].outerHTML = someArray[i].outerHTML;
  84. //console.log("after");
  85. //console.log(temp);
  86. //console.log(someElement[2].outerHTML);
  87. //console.log(someArray[2].outerHTML);
  88. }
  89. }
  90. catch(err) {
  91. console.log("Error occured");
  92. console.log(err);
  93. }
  94. };

comments powered by Disqus