# Replace a node using tr.replaceWith - how to find node position?

**URL:** <https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388>\
**Category:** Uncategorized\
**Created:** [August 22, 2016, 6:32pm UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388 "2016-08-22T18:32:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapouer](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kapouer/32/227_2.png) [@kapouer](https://discuss.prosemirror.net/u/kapouer)\
**Post date:** [August 22, 2016, 6:32pm UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/1 "2016-08-22T18:32:57Z")

</div>

I use `transformPasted` event to return a new Slice with some custom nodes in it. Such custom nodes load remote data to set attributes, so first i create them and return them in the slice returned in the event subscriber, then load remote data, then i think the right way to update them is:

`pm.tr.replaceWith(pos, pos+1, schema.types.mynode.createAndFill(...)).apply()`

But i don’t find how to get a node position. Is there some way i overlooked in the 0.9 reference ?

---

<div class="post-metadata">

**Author:** ![kapouer](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kapouer/32/227_2.png) [@kapouer](https://discuss.prosemirror.net/u/kapouer)\
**Post date:** [August 23, 2016, 12:12am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/2 "2016-08-23T00:12:42Z")

</div>

Found a way to workaround this by setting an id on the node and getting the position using

`var node = document.getElementById(id); var pos = pm.posFromDOM(node); var begin = pos.pos; var $pos = pm.doc.resolve(begin); var end = begin + $pos.nodeAfter.nodeSize; pm.tr.replace(begin, end, ...);`

but it feels ugly. I don’t understand yet how to use a marked range but i suppose it’s the way to go.

---

<div class="post-metadata">

**Author:** ![kiejo](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kiejo/32/64_2.png) [@kiejo](https://discuss.prosemirror.net/u/kiejo)\
**Post date:** [August 23, 2016, 10:49am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/3 "2016-08-23T10:49:48Z")

</div>

I think you could use a `MarkedRange` in the following way:

```javascript
// Create the node without the attributes
pm.tr.insertInline(pos, schema.types.mynode.createAndFill(...)).apply();
// Create a marked range for the node's position
const markedRange = pm.markRange(pos, pos + 1);

// Load data from remote...

// Update your node with the retrieved attributes
pm.tr.setNodeType(markedRange.from, null, attributes).apply();
// Clean up
pm.removeRange(markedRange)

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![kapouer](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kapouer/32/227_2.png) [@kapouer](https://discuss.prosemirror.net/u/kapouer)\
**Post date:** [August 23, 2016, 4:04pm UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/4 "2016-08-23T16:04:05Z")

</div>

Yes, this helps. Unfortunately i think i can’t make it to play well with transformPasted event subscriber: it returns a slice that is not yet inserted in the document and unless there’s a way to mark a range in that slice (which doesn’t make any sense to me) i’m still stuck with my “bad” solution.

---

<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:** [August 26, 2016, 9:56am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/5 "2016-08-26T09:56:48Z")

</div>

What if you give your placeholder node some kind of id attribute, and let the asynchronous action that is supposed to complete it search for that id?

---

<div class="post-metadata">

**Author:** ![kapouer](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kapouer/32/227_2.png) [@kapouer](https://discuss.prosemirror.net/u/kapouer)\
**Post date:** [August 26, 2016, 10:54am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/6 "2016-08-26T10:54:36Z")

</div>

Yes, that’s what i did. You can see it in action there: [http://kiwi.eda.sarl/~eda/sample/index.html](http://kiwi.eda.sarl/~eda/sample/index.html) (paste a url into the editor).

---

<div class="post-metadata">

**Author:** ![kiejo](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kiejo/32/64_2.png) [@kiejo](https://discuss.prosemirror.net/u/kiejo)\
**Post date:** [August 26, 2016, 4:21pm UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/7 "2016-08-26T16:21:25Z")

</div>

> [@kapouer](#):
>
> Unfortunately i think i can’t make it to play well with transformPasted event subscriber

You’re right, I hadn’t thought of this! Alternatively, I think the newly introduced [domPaste](http://prosemirror.net/ref.html#ProseMirror.on.domPaste) event could help in that case if you wanted to go with a `MarkedRange`. Of course if your current approach with the id works well, there’s not really a need to change it.

---

<div class="post-metadata">

**Author:** ![Leecason](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/leecason/32/833_2.png) [@Leecason](https://discuss.prosemirror.net/u/Leecason)\
**Post date:** [March 5, 2020, 6:15am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/8 "2020-03-05T06:15:54Z")

</div>

Can you share the source code of this demo? I have the same problem now, and I want to replace the text of the link with remote data after pasting. thank you

---

<div class="post-metadata">

**Author:** ![Bulala-liu](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bulala-liu/32/3108_2.png) [@Bulala-liu](https://discuss.prosemirror.net/u/Bulala-liu)\
**Post date:** [October 11, 2022, 4:08am UTC](https://discuss.prosemirror.net/t/replace-a-node-using-tr-replacewith-how-to-find-node-position/388/9 "2022-10-11T04:08:54Z")

</div>

the link is 404, can you share a valid demo? Thanks a lot!
