views:

31

answers:

1

In prototypejs, why does the following code remove the matching divs from the #test div?

What confuses me is that this happens when they are being inserted in the #droparea, and not when they are being pushed in the array.

<div id="test">
    <div class="foo" id="22.1234">
        1
    </div>
    <div class="foo" id="22.1235">
        2
    </div>
    <div class="foo" id="53.2345">
        3
    </div>
    <div class="foo" id="53.2346">
        4
    </div>
</div>

<div id="droparea">

</div>

js

var elArray = [];
var els = $('test').select('.foo');

els.each(function(x){ if(x.id.split('.')[0] == 22){ elArray.push(x); } });
elArray.each(function(y){ $('droparea').insert({ bottom: y }); });
+3  A: 

I take it you want to copy/clone the elements into the drop area, not move them?

This thread on Google Groups discusses how to clone an element. Note in particular the caveats about changing the ID before reinserting to the document.

nickf
Works for me, but I still don't get it why it behaves like this :)
koko
Because there's only one element, and you've just pushed it into the other area. When you store an element in an array, you're only storing a reference to it, not a copy of it.
nickf