Why does jquery create several duplicates?
<p>This code is run several times, each time 'this' points to a the dom element that I receive from the firing event. First time I run the code I have 0 extra dom elements, next time I get 1 more dom element each time the code runs. But since I'm removing an element each time I clone it and after is only supposed to move an element to a different place I don't understand why I keep getting more elements, am I missing something?</p>

<pre><code>div = $(this); //I found that this and $(this) were different
dummy = div.clone();
//Insert clone after original:
div.parent().children().eq(div.parent().children().index(div)).after(dummy);
//Insert original after clone:
dummy.parent().children().eq(dummy.parent().children().index(dummy)).after(div);
dummy.remove();
</code></pre>

<p>Each time the code above runs, every time with 'this' being the same dom, I get more duplicates of that dom in the list. The first time the code runs I get 0 extra doms, as should be since I'm removing what I duplicated, but after that for some reason I'm getting more doms.</p>

<p>Edit: Oops, I was wrong, the number of extra dummy elements is 2^n-1, where n is the number of times the code runs. I'm new to jquery, something fishy is going on.</p>

<p>Later Edit: OK once again I apologize for asking the question, I've found the problem. The problem was that the event handler was called multiple times and I didn't include that code above. But my code is a mess right now since I'm just in the process of learning jquery.</p>
