question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get a selected text?

See original GitHub issue

If I have next text in the editor

Example text here
Another paragraph

And when I select word text and print to console state.focusText.text - I got whole Example text here sentence. Bu I wan to get text word instead. I tried bunch of methods - but no one seems to work.

I guess I can get selected work by using state.startOffset and state.endOffset properties ( or state.focusKey) - but I don’t know how.

Slate should have some convenient method for this case. Am I right?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

37reactions
5-10commented, Nov 15, 2020

There’s Editor.string function that can be used to get the text at the selection.

const selectedText = Editor.string(editor, editor.selection);

25reactions
linonetwocommented, Mar 29, 2020

It’s now value.fragment.text, where value is from change.value.


It seems to be Node.texts(root: Node, options?): Iterable<NodeEntry<Text>> now

https://docs.slatejs.org/api/nodes#static-methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the Highlighted/Selected text - javascript - Stack Overflow
Getting the text the user has selected is relatively simple. ... function getSelectionText() { var text = ""; var activeEl = document.
Read more >
How to get the Highlighted/Selected text in JavaScript?
There may be a need to find out the text selected/highlighted by the user. It can be done very easily using the window...
Read more >
How do I get the text value of a selected option?
Select elements typically have two values that you want to access. First there's the value ... The second is the text value of...
Read more >
Window.getSelection() - Web APIs | MDN
The Window.getSelection() method returns a Selection object representing the range of text selected by the user or the current position of ...
Read more >
get selected text with JavaScript - CodePen
1. <p onmouseup="getSelectedText(); alert(txt)"> ; 2. Highlight some of this text ; 3. with the mouse select press button end release ; 4....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found