How do I move a div after its next div. For instance I have 3 different div sitting here.
<div class="div1">Div1</div>
<div class="div2">Div2</div>
<div class="div3">Div3</div>
I wan to move first div after the second div. It means div2
will be first and div1
will be second. I have bunch of same html format.
I want to execute jquery something like
$(".div1").each(function() {
$(this).appendTo().$(this).after();
});
Let me know if this doesn't make sense.
you can get the .next() element, and place it .after()
or
you can .insertAfter() the .next() element
$(".div1").each(function() {
var item = $(this);
//either this:
item.next().after(item);
//or this:
item.insertAfter(item.next());
});
No need for the "each" right - Jargs 2015-04-17 22:35
Here is how you do cut paste operations with DOM in jQuery way.
Var temp= $(“div1”).detach(); //Performs the cut operation
temp.insertAfter(“div2”); //Does the paste
detach
, just $('.div1').insertAfter('.div2');
is sufficient: http://jsfiddle.net/ambiguous/LFqG3 - mu is too short 2012-04-04 04:23
So lets say you have written a code for mousein and mouseout events using jQuery (can be counted as all jQuery data associated) , that handler would behave erratically if you don't use detach. Why do you think that jQuery folks would include a useless method and that too when you have remove() to perform the cut operation alone - anuj_io 2012-04-04 04:42
$('.div1').insertAfter('.div2');
preserves the bindings unless I'm missing something: http://jsfiddle.net/ambiguous/LFqG3/1 - mu is too short 2012-04-04 04:57