sorting of div's using javascript

T

TKapler

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?
 
M

Matt Kruse

TKapler said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top