# Approaches for finding the position of nodes

**URL:** <https://discuss.prosemirror.net/t/approaches-for-finding-the-position-of-nodes/3333>\
**Category:** Uncategorized\
**Created:** [December 7, 2020, 4:28pm UTC](https://discuss.prosemirror.net/t/approaches-for-finding-the-position-of-nodes/3333 "2020-12-07T16:28:08Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Pier](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/pier/32/616_2.png) [@Pier](https://discuss.prosemirror.net/u/Pier)\
**Post date:** [December 9, 2020, 10:23pm UTC](https://discuss.prosemirror.net/t/approaches-for-finding-the-position-of-nodes/3333/5 "2020-12-09T22:23:09Z")

</div>

After numerous attempts I think I’m **finally** getting the hang of this.

For example, to get the start/end positions of the previous node in the document:

```auto
// first get the index of the current node at the root level
const parentIndex = $cursor.index(0);

// iterate the doc to find the offset of the previous index
let startPos, endPos;

doc.forEach((node, offset, index) => {
    if (index === parentIndex - 1) {
        startPos = offset;
        endPos = offset + node.nodeSize;
    }
});

```

Or to get the positions of the last child of the previous block:

```auto
doc.forEach((node, offset, index) => {
    if (index === parentIndex - 1) {
        node.forEach((childNode, childOffset, childIndex) => {
            if (childIndex === node.childCount - 1) {
                startPos = offset + childOffset;
                endPos = startPos + node.nodeSize;
            }
        })
    }
});

```

I tried many attempts which all ended up using `node.eq(anotherNode)` to determine if I had “found” the node I was interested in, but as @marijn has stated this is a bad idea. When iterating it’s possible you could get a different node if these are identical.

I also tried resolving positions inside a node (other than the document) but this was useless. All transforms require absolute global positions and I couldn’t find a way to “translate” a local position to a global one.

---

_[View the full topic](https://discuss.prosemirror.net/t/approaches-for-finding-the-position-of-nodes/3333)._
