Dan Inactive
Dan Inactive

Reputation: 10060

jquery: getting elements from xml in different order

The xml response from the ajax request looks like this:

<element type='type1' />
<element type='type2' />
...
<element type='type1' />

I want to insert dom elements based on the xml, but I want them to be ordered as: first all the elements of type1, then all the elements of type2.

It made sense for me (and I also read some discussions on the web) that a compound selector would do it:$(xml).find('element[type=type1], element[type=type2]').each(). Unfortunately this still gets the element in the xml order.

Any ideas?

Upvotes: 0

Views: 835

Answers (2)

jwl
jwl

Reputation: 10514

var arr=$(xml).find('element[type=type1]').get().concat(
           $(xml).find('element[type=type2]').get()
         );

that gives you an array which is ordered how you want. Then I think you can:

$(arr).each(); 

and do as you please

Upvotes: 1

Marc W
Marc W

Reputation: 19241

What about just selecting the types separately? Instead of pushing them all into one selector, just have two statements and iterate through them separately.

$(xml).find('element[type=type1]').each( ... );
$(xml).find('element[type=type2]').each( ... );

Or is that not possible for some other reason?

Upvotes: 0

Related Questions