# Find the closest node of certain type from a specific position

**URL:** <https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068>\
**Category:** Uncategorized\
**Created:** [December 1, 2022, 9:09pm UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068 "2022-12-01T21:09:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MakarovCode](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/makarovcode/32/2989_2.png) [@MakarovCode](https://discuss.prosemirror.net/u/MakarovCode)\
**Post date:** [December 1, 2022, 9:09pm UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/1 "2022-12-01T21:09:19Z")

</div>

Hi people, I need to get the closest node of a particular type from a known position.

So let’s say I have the following nodes

image **abc** paragraph(1) paragraph(2)

I need to know from both paragraph which the closest **abc** node is in order to perform certain logic

So I was planning to loop recursively using nodeBefore method until I found the **abc** node, but just wondering if there is a better way.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [December 2, 2022, 11:13am UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/2 "2022-12-02T11:13:18Z")

</div>

Something like this should work.

```javascript
let found = -1
doc.nodesBetween(pos, doc.content.length, (node, pos) => {
  if (found > -1) return false
  if (isMyNode(node)) found = pos
})
if (found > -1) console.log("there's a node at", found)

```

---

<div class="post-metadata">

**Author:** ![MakarovCode](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/makarovcode/32/2989_2.png) [@MakarovCode](https://discuss.prosemirror.net/u/MakarovCode)\
**Post date:** [December 2, 2022, 7:59pm UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/3 "2022-12-02T19:59:28Z")

</div>

Thanks worked like a charm!

---

<div class="post-metadata">

**Author:** ![tslocke](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/tslocke/32/2218_2.png) [@tslocke](https://discuss.prosemirror.net/u/tslocke)\
**Post date:** [January 30, 2023, 11:48am UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/4 "2023-01-30T11:48:14Z")

</div>

I’m really confused by this question/answer. Isn’t the need to search _backwards_ (“using nodeBefore”) from a given paragraph to find the “abc”?

But the nodesBetween is starting from `pos` (which I assume to be the initial pos we’re searching from?) and scans _forwards_ towards `doc.content.length`.

But @MakarovCode is happy so I am misunderstanding something!

---

<div class="post-metadata">

**Author:** ![MakarovCode](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/makarovcode/32/2989_2.png) [@MakarovCode](https://discuss.prosemirror.net/u/MakarovCode)\
**Post date:** [January 30, 2023, 2:29pm UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/5 "2023-01-30T14:29:10Z")

</div>

Hey @tslocke, I adapted this logic to work to my needs, but the general idea of the shared code helped me with the solution, on the specific case of looking backward I ended up searching from position 0 up to the node in question.

````auto
let found = -1

  editor.view.state.doc.nodesBetween(0, pos, (node, pos) => {

    if (isMyNode(node)) {
      found = pos;
    }
  })
  if (found > -1) {
    return {pos: found}
  }
  return null```
````

---

<div class="post-metadata">

**Author:** ![tslocke](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/tslocke/32/2218_2.png) [@tslocke](https://discuss.prosemirror.net/u/tslocke)\
**Post date:** [January 30, 2023, 3:04pm UTC](https://discuss.prosemirror.net/t/find-the-closest-node-of-certain-type-from-a-specific-position/5068/6 "2023-01-30T15:04:56Z")

</div>

Ahh thanks - makes sense now : )
