[Javascript] I have hit a brick wall

Jonathan Buchanan jonathan.buchanan at gmail.com
Thu Sep 7 11:18:51 CDT 2006


On 9/7/06, Terry Riegel <riegel at clearimageonline.com> wrote:
> Thanks Jonathan,
>
> That works for the array, but I need to swap out the objects
> represented by the ID. I need soemthing similar to the swapNodes()
> function you wrote yesterday.
>
> This is my humble (non-working) attempt so far.
>
>
> function rippleNodes(a,k)
> {
>     var placeHolder = document.createElement(k);
>     var i=document.getElementById(a[0])
>     for (x=0; x<a.length; x++)
>      {
>      if (x==0)
>       {
>        i.parentNode.replaceChild(placeHolder,i)
>       }
>      else
>       {
>        var n=document.getElementById(a[x])
>        var n1=document.getElementById(a[x-1])
>        n.parentNode.replaceChild(n1,n)
>       }
>      }
>     placeHolder.parentNode.replaceChild(i, placeHolder);
> }
>
>
> Terry

The implementation of this really depends on the specifics of how the
document is structured. Moving each and every node one at a time is a
last resort, as you can structure your document in a way which makes
it easy to manipulate as you desire.

For example, if the nodes in question are arranged like so:

parent
|-- child1
|-- child2
|-- child3

...then moving child1 to the end is as simple as
child1.parentNode.appendChild(child1).

Similarly, if each node you're interested in is wrapped in its own
container element, but the container elements are the only children of
some common parent element, you do exactly the same, but operating on
child1.parentNode instead of child1.

Is for the same page as before?

Jonathan.



More information about the Javascript mailing list