By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

sorting of div's using javascript

P: n/a
I need a javascript for sorting DIV's based on their ID. some of them
may be nested (all of them have the same class)
e.g. my structure:

'
DIV ID="abc-123" class="myclass"
DIV ID="abc-127" class="myclass"
/DIV
/DIV
DIV ID="abc-124" class="myclass"
/DIV
DIV ID="abc-125" class="myclass"
DIV ID="abc-126" class="myclass"
/DIV
/DIV

and I need a script to cut out all "myclass" divs and place them on the
same position but sorted and unnested like this:
DIV ID="abc-123" class="myclass"
/DIV
DIV ID="abc-124" class="myclass"
/DIV
DIV ID="abc-125" class="myclass"
/DIV
DIV ID="abc-127" class="myclass"
/DIV
DIV ID="abc-128" class="myclass"
/DIV

Can you help me how to do it?

Nov 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
TKapler wrote:
I need a javascript for sorting DIV's based on their ID. some of them
may be nested (all of them have the same class)
and I need a script to cut out all "myclass" divs and place them on
the same position but sorted and unnested like this:
Use getElementsByTagName to scan through the list of DIV's.
Inspect each classname. If it's "myclass" then push its ID on to an array.
Sort the array.
Loop through the array and use getElementById to grab a reference to each
DIV
Use appendChild on your target element and pass each DIV, which will
automatically remove it from its previous location.

Hope that helps!

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.