Does the order of elements in a jQuery-wrapped set always match the order in which the elements appear in the markup?

Just been looking at this myself. jQuery does return things in document order as per the following article:

http://docs.jquery.com/Release:jQuery_1.3.2

So, if you select some ids as such:

$("#id1, #id2, #id3")

Then they will be returned in the order they appear in the DOM, not necessarily in the order they are given. Its certainly worth being aware of this when you upgrade from earlier to later version of jQuery.


jQuery will match your query top down so the matched set will always be the same.


I forget which specific version it was, but JQuery 1.3.x guaranteed that the document order was the order of the resulting elements, even with queries like 'span, div'. (Prior versions did not return the results in document order, and I have needed this several times.)

Tags:

Dom

Jquery