---
title: "traverseChildren"
description: "Executes [block] for all direct children of the node with a matching [key].

Note 1: This stops at the children and does not include grandchildren and so on down the tree.

Note 2: The parameter [block]'s return boolean value will determine if the traversal will
continue (true = continue, false = cancel)."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun DelegatableNode.traverseChildren(key: Any?, block: (TraversableNode) -> Boolean)
```


Executes `block` for all direct children of the node with a matching `key`.

Note 1: This stops at the children and does not include grandchildren and so on down the tree.

Note 2: The parameter `block`'s return boolean value will determine if the traversal will
continue (true = continue, false = cancel).



<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun <T> T.traverseChildren(block: (T) -> Boolean) where T : TraversableNode
```


Executes `block` for all direct children of the node that are of the same class.

Note 1: This stops at the children and does not include grandchildren and so on down the tree.

Note 2: The parameter `block`'s return boolean value will determine if the traversal will
continue (true = continue, false = cancel).



## Code Examples
### traverseChildrenDemo
```kotlin
/**
 * Simplified example of traverseChildren. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */
fun traverseChildrenDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()
    with(customTraversableModifierNode) {
        traverseChildren {
            // Because I use the existing key of the class, I can guarantee 'it' will be of the same
            // type as the class, so I can call my functions directly.
            it.doSomethingWithChild()
            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}
```
### traverseChildrenWithKeyDemo
```kotlin
/**
 * Simplified example of traverseChildren with a key. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */
fun traverseChildrenWithKeyDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()
    with(customTraversableModifierNode) {
        traverseChildren(traverseKey) {
            if (it is CustomTraversableModifierNode) {
                it.doSomethingWithChild()
            }
            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}
```

