How do I move an HTML element in jQuery?

You can use .insertBefore(), like this:

$("#2").insertBefore("#1");

Or, .prependTo(), like this:

$("#2").prependTo("#parent");

…or the reverse using #1 and .insertAfter() and .appendTo()…or several other ways actually, it just depends what you’re actually after, the above 2 methods should be about the shortest possible though, given 2 IDs.

I’m assuming this is just an example, remember to use IDs that don’t start with a number in an actual HTML4 page, they’re invalid and cause several issues.

Leave a Comment