# Setting child index of unknown child

**URL:** <https://forum.kirupa.com/t/setting-child-index-of-unknown-child/290564>\
**Category:** flash\
**Created:** [June 16, 2009, 2:18pm UTC](https://forum.kirupa.com/t/setting-child-index-of-unknown-child/290564 "2009-06-16T14:18:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BoppreH](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@BoppreH](https://forum.kirupa.com/u/BoppreH)\
**Post date:** [June 16, 2009, 2:18pm UTC](https://forum.kirupa.com/t/setting-child-index-of-unknown-child/290564/1 "2009-06-16T14:18:23Z")

</div>

I want to bring a display object to a specific position knowing only the child itself and the uppermost container whose indexes will have to be changed. Here’s the setup:

```auto
container
- child1
-- subchild1
-- subchild2
- child2
-- subchild3
-- subchild4
-- subchild5
[...]
- child3
-- subchild23
-- subchild24

```

I know that _container_ contains an interesting child nested inside itself. Let’s call it _subchild15_, but it actually can be even more nested.

How to I make sure that _subchild15_ appears (via child index) between \*child2 \*and _child1_ without knowing it’s closest parent to _container_?

I can only think of the following method:

```auto
function getClosestChildThatContains(container, nestedChild) {
    for (var i=0; i < container.numChildren; i++) {
        var child = container.getChildAt(i)
        if (child == nestedChild)
            return child
        else if (child.contains(nestedChild))
            return getClosestChildThatContains(child, nestedChild)
    }
}
trace(getClosestChildThatContains(container, subchild15))

```

But it looks painfully complex and slow. Speed is not really a problem in this case, the function will be called only once every few seconds (for now), but I was wondering if there is anything more elegant to do in this case.
