Hi,

FWIW, I think you can avoid the branching.

To remember an element's position and then remove it:
* * * *
function saveAndRemoveElement(elm) {
    var saved;

    saved = {
        elm:    elm,
        parent: elm.parentNode,
        next:   elm.nextSibling  // <= May be undefined, that's okay
    };
    elm.remove();
    return saved;
}
* * * *

To restore it:
* * * *
function restoreSavedEelement(saved) {
    saved.parent.insertBefore(saved.elm, saved.next);
}
* * * *

Node#insertBefore inserts at the end of the second parameter is
undefined.

The only Prototype-ism in the above is elm.remove().

Hope this helps,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Feb 18, 8:40 pm, Aitor Garay-Romero <aito...@gmail.com> wrote:
> Hi there!
>
>   I need to remove an arbitray element from the DOM and add it later
> exactly to the same point in the DOM.  I wonder is there is a direct
> way to achieve this.  Right now i have to check wether the element has
> a previous sibling or directly a parent and remember this information
> to add it back.
>
>   When i remove i do something like:
>
>   var e = $( ...),
>       previousSiblings = e.previousSiblings();
>
>   if( previousSiblings.size() > 0) {
>     this._insertPoint = previousSiblings.first();       // it has a sibling
>   } else {
>     this._insertPoint = e.parentNode;           // no sibling, use parent as
> reference
>     this._insertPointIsParent = true;
>   }
>
>   And to add it back:
>
>   var e = $( ...);
>
>   if( this._insertPointIsParent) {
>     this._insertPoint.insert( { top: e});
>   } else {
>     this._insertPoint.insert( { after: e});
>   }
>
>   So, there is any direct way to do this or i have to use this
> algorithm?
>
>   Thanks!
>
>   /AITOR
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to