Let’s say I have three <div>
elements on a page. How can I swap positions of the first and third <div>
? jQuery is fine.
Advertisement
Answer
Trivial with jQuery
JavaScript
x
3
1
$('#div1').insertAfter('#div3');
2
$('#div3').insertBefore('#div2');
3
If you want to do it repeatedly, you’ll need to use different selectors since the divs will retain their ids as they are moved around.
JavaScript
1
7
1
$(function() {
2
setInterval( function() {
3
$('div:first').insertAfter($('div').eq(2));
4
$('div').eq(1).insertBefore('div:first');
5
}, 3000 );
6
});
7