Search code examples
javascriptjqueryxmldominverse

Inversing DOM with jQuery


Input:

<content>
   <dom id=1>a</dom>
   <dom id=2>b</dom>
   <dom id=3>c</dom>
   <dom id=4>d</dom>
</content>

Output:

<content>
   <dom id=4>d</dom>
   <dom id=3>c</dom>
   <dom id=2>b</dom>
   <dom id=1>a</dom>
</content>

I'm looking for a function (if exists) like this:

$('content').inverse();


Solution

  • Something like this would work:

    $(​$("content dom").get().reverse()).appendTo("content");​​​​​​​​
    

    You can test it out here, or if you like the reverse flavor:

    $("content").append($("content dom").get().reverse());​
    

    Try that version here.