# Retrieve selected node start/end positions

**URL:** <https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218>\
**Category:** Uncategorized\
**Created:** [March 5, 2016, 9:07am UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218 "2016-03-05T09:07:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://discuss.prosemirror.net/u/paul)\
**Post date:** [March 5, 2016, 9:07am UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/1 "2016-03-05T09:07:11Z")

</div>

Hi,

I would like to change mark’s attribute, at current caret position, when just clicking on text.

My question is in the code below :

```
pm.on('selectionChange', () => {
  let sel = pm.selection;
  if (sel.from && sel.to) {
    // if selection length is 0 (just clicked on text) 
    if (sel.from.cmp(sel.to) === 0 ) {
      let node = pm.doc.nodeAfter(sel.from);
      if (node) {
        // my question is here
        // how to retrieve node position start and node position end
        // to replace mark at selection pos
      }
    }  
  }
}
```

---

<div class="post-metadata">

**Author:** ![mattberkowitz](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/mattberkowitz/32/126_2.png) [@mattberkowitz](https://discuss.prosemirror.net/u/mattberkowitz)\
**Post date:** [March 5, 2016, 7:02pm UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/2 "2016-03-05T19:02:04Z")

</div>

I’ve done something like this:

```auto
let start
if(node.isTextblock) {
  start = sel.from.shorten()
} else {
  start = sel.from
}
let end = start.move(1)

```

(I think that’s right, I’m going off of pure memory here)

The reason being, if your selection is in a text block the path component of `from` is the path to the node and the offset is the position within the text, but for a block selection the path component is the path to the parent node and the offset is the blocks position in relation to it’s siblings

---

<div class="post-metadata">

**Author:** ![paul](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://discuss.prosemirror.net/u/paul)\
**Post date:** [March 6, 2016, 8:50am UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/3 "2016-03-06T08:50:24Z")

</div>

Maybe i didn’t explain well what i’m looking for. Let’s take an example with this document :

> Hello ! **Bold text** _and italic text_.

The 3 text nodes are :

- Hello !\_ (width 8)
- Bold text (width 9)
- and italic text (width 15)

I would like to have :

- if I click anywhere on node “Hello !” =\> startingpos : [0:0], endpos : [0:8]
- if I click anywhere on node “Bold text” =\> startingpos: [0:8], endpos [0:17]
- if I click anywhere on last node =\> startingpos [0:17] endpos [0:32]

Thanks.

---

<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:** [March 7, 2016, 3:21pm UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/4 "2016-03-07T15:21:49Z")

</div>

This is currently a bit of a pain, but you can use [`chunkBefore`](http://prosemirror.net/ref.html#Node.chunkBefore) or [`chunkAfter`](http://prosemirror.net/ref.html#Node.chunkBefore) (depending on which directly you want to bias towards) to get the start of the text node at a given position.

---

<div class="post-metadata">

**Author:** ![redFur](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/redfur/32/760_2.png) [@redFur](https://discuss.prosemirror.net/u/redFur)\
**Post date:** [December 16, 2018, 4:56pm UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/5 "2018-12-16T16:56:29Z")

</div>

Hi, I have this exact problem. Is there a new way to do this? The links don’t seem to lead anywhere anymore.

---

<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 16, 2018, 6:29pm UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/6 "2018-12-16T18:29:04Z")

</div>

You’ll want to use a [resolved position](http://prosemirror.net/docs/ref/#model.ResolvedPos) for this in the current interface.

---

<div class="post-metadata">

**Author:** ![redFur](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/redfur/32/760_2.png) [@redFur](https://discuss.prosemirror.net/u/redFur)\
**Post date:** [December 16, 2018, 6:51pm UTC](https://discuss.prosemirror.net/t/retrieve-selected-node-start-end-positions/218/7 "2018-12-16T18:51:48Z")

</div>

Thanks for the fast response! I see textOffset there which I think gets me where I want.
