# How to set initial focus of document

**URL:** https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476
**Category:** Uncategorized
**Created:** [February 5, 2021, 8:44am UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476 "2021-02-05T08:44:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)
#### Post date: [February 5, 2021, 8:44am UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476/1 "2021-02-05T08:44:48Z")

</div>

Hey there,

I want set the initial cursor in the document at the end of the first paragraph. Here’s my code so far:

```
	const doc = schema.nodeFromJSON({ type: "doc", content: [{ type: "paragraph" }] })
	const state = EditorState.create({/* ... */})
	const view = new EditorView<EditorSchema>(node, {/* ... */})

	// Focus
	node.childNodes[0].focus()

	// Error: Selection passed to setSelection must point at the current document
	view.dispatch(
		view.state.tr.setSelection(
			TextSelection.atEnd(view.state.tr.doc.firstChild)
		)
	)

```

I’m seeing a weird error having to do with the doc being used for the selection is not the same on for the transaction? It’s a bit unclear to me what I’m supposed to do differently though.

 ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/3/3df88052f34c06ab387abe4cd95fcf4c82bfa2f2.png)

Any thoughts?

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: [February 5, 2021, 9:47am UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476/2 "2021-02-05T09:47:25Z")

</div>

Does your state initialization set the state’s doc to `doc`? In any case, you’ll probably want to pass the new selection right away when creating the state, and call `view.focus()` instead of just the DOM focus method.

---

<div class="post-metadata">

### Author: ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)
#### Post date: [February 5, 2021, 6:28pm UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476/3 "2021-02-05T18:28:22Z")

</div>

Ah, I get it now.

Calling `node.childNodes[0].focus()` changes the selection of the view which means that the state has changed by the time I call setSelection.

This appears to work without that error.

```
const state = EditorState.create({
    selection: TextSelection.atEnd(doc.firstChild!),
    /* ... */
})
view.focus()

```

However, I think I may have found a bug… This code places the cursor at one character before the end 🤔

This is the code I have:

```
const doc = schema.nodeFromJSON(initialDocJson)

const state = EditorState.create({
	selection: TextSelection.atEnd(doc.firstChild!),
	schema: schema,
	doc: doc,
	plugins: [
		history(),
		keymap({ "Mod-z": undo, "Mod-y": redo }),
		keymap({ "Mod-b": toggleMark(schema.marks.bold) }),
	],
})

```

And here is the result: [ProseMirror Examples](https://ccorcos.github.io/prosemirror-examples/#/focus)

You can repro yourself as well:

```
git clone git@github.com:ccorcos/prosemirror-examples.git
cd prosemirror-examples
npm install
npm start
open http://localhost:8080/#/focus
```

---

<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: [February 8, 2021, 11:15am UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476/4 "2021-02-08T11:15:48Z")

</div>

> [@ccorcos](#):
>
> `selection: TextSelection.atEnd(doc.firstChild!)`

`atEnd` expects the entire document, not any node (it creates a selection relative to the document it was given, which as you found doesn’t work if it only uses a given node).

---

<div class="post-metadata">

### Author: ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)
#### Post date: [February 8, 2021, 8:41pm UTC](https://discuss.prosemirror.net/t/how-to-set-initial-focus-of-document/3476/5 "2021-02-08T20:41:03Z")

</div>

Ah, that makes sense. Thanks
