Lohmeyer Posted February 14, 2010 Share Posted February 14, 2010 hiim trying to sort a set of html tags and i named them all "t"<input type="hidden" name="t" id="id1" title="some title" value="some numeric value" /><input type="hidden" name="t" id="id2" title="some title" value="some numeric value" /><input type="hidden" name="t" id="id3" title="some title" value="some numeric value" />and i put them all on my var x = document.getElementsByName("t")now i want to sort them on var x according to their value (descending).. and i want the actual elements to switch places not just the values they carry coz i need some of their html attributes to be preserved..ive made a little code but it doesnt seem to work.. any help would be appreciated..for(var i = 1; i < x.length; i++) { if(x.value > x[i-1].value) { var tmp = x[i-1]; x[i-1] = x; x = tmp; } }this doesnt work...any help? Link to comment Share on other sites More sharing options...
jeffman Posted February 14, 2010 Share Posted February 14, 2010 Be careful with getElementsByName. It's a recent addition to the DOM, and older browsers may not support it.In any case, methods like that do not return true arrays, They return dynamic node collections. That means they do not support most array methods and properties, and elements cannot be reassigned. To do that kind of stuff, you'd need to copy the collection into an array.This is why it might be easier to build the array using a different technique in the first place.Once you have an array, you might look at JavaScript's built-in array.sort() method. You'll need to use a hook function so that sort operates on the element values instead of the elements themselves.Try this: function mySort () { function byElValue (a, b) { return a.value - b.value; } var inputs = document.getElementsByTagName('input'); var textinputs = []; var len = inputs.length; for (var i = 0; i < len; ++i) { if (inputs[i].type.toLowerCase() == "text") { textinputs.push(inputs[i]); } } return textinputs.sort(byElValue);} Note that this only gives you a sorted array. It does not rearrange the elements on the page. That's a whole other thing. But the array is the correct way to begin, if that's what you want. Link to comment Share on other sites More sharing options...
Lohmeyer Posted February 14, 2010 Author Share Posted February 14, 2010 thats exactly what i wanted..thanks!basically copy the collection into a true array and then use the sort function.. no wonder i cant use push, shift and unshift earlier..the only modifications i made wasb.value - a.value to make it descendingand the filtering inputs[i].type.toLowerCase() == "text" into inputs[i].name == "t" to capture only those i wantgreat help! thanks alot! Link to comment Share on other sites More sharing options...
jeffman Posted February 14, 2010 Share Posted February 14, 2010 I spent a really agonizing afternoon some time back figuring our that node collections don't have array properties. That's my role here. I endured the pain so you don't have to. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.