Is there a native jQuery function to switch elements?

juan picture juan · Mar 30, 2009 · Viewed 123.8k times · Source

Can I easily swap two elements with jQuery?

I'm looking to do this with one line if possible.

I have a select element and I have two buttons to move up or down the options, and I already have the selected and the destination selectors in place, I do it with an if, but I was wondering if there is an easier way.

Answer

lotif picture lotif · Dec 26, 2011

I've found an interesting way to solve this using only jQuery:

$("#element1").before($("#element2"));

or

$("#element1").after($("#element2"));

:)