getSelection() only works after setSelection()

See original GitHub issue

When I try to use the API function getSelection(), it gives an error: TypeError: quill.getSelection(...) is null However, if I first set the selection (shows for the user) quill.setSelection(3,5); alert(quill.getSelection().end); It gives no error, and displays a popup saying ‘5’

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

23reactions
willrowecommented, Jun 5, 2015

It turned out that the issue was that the editor was losing focus when clicking on a custom button. This can be resolved by adding a custom format tied to a button or by calling quill.focus before quill.getSelection. A demo can be seen here

0reactions
shreedharancommented, Oct 26, 2020

I want to set cursor to specific position but not working in my code here is the sample code I wrote any body know please update

import React, {Component} from ‘react’; import ReactQuill from ‘react-quill’ import “react-quill/dist/quill.snow.css”; import ‘./App.css’;

class App extends Component{ constructor(props) { super(props); this.state = { editorHtml: “testing” }; this.handleChange = this.handleChange.bind(this); this.handleChangeSelection = this.handleChangeSelection.bind(this);

this.reactQuillRef = null

}

handleChange(content, delta, source, editor) { console.log(content); console.log(delta); console.log(source); console.log(editor.getSelection()); //console.log(“IBM”+editor.getHTML()); // HTML/rich text //alert(event); //this.setState({ editorHtml: editor.getHt }); }

handleChangeSelection(e) { console.log(e); }

moveCursor = () => { this.reactQuillRef.focus();

console.log(this.reactQuillRef.getEditorSelection().index);
//console.log(this.reactQuillRef);

var range = this.reactQuillRef.getEditorSelection().index;

}

render(){ return ( <div className="App">

    <div className="text-editor">        
    <ReactQuill         
      ref={(el) => { this.reactQuillRef = el }}
      autoFocus="true"         
      onChange={this.handleChange}
      onChangeSelection={this.handleChangeSelection}
      value={this.state.editorHtml}             
      theme={"snow"} // pass false to use minimal theme
    />
    </div>
    <div className="row"><button onClick={() => this.moveCursor()}>Move Curser</button></div>
  </div>
);

} }

export default App;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window.getSelection changes after being set to another ...
I seem to have a problem that my window.getselection object changes after I set it to another object. Here is an example: document....
Read more >
Window.getSelection() - Web APIs | MDN
getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.
Read more >
Selection and Range - The Modern JavaScript Tutorial
The document selection is represented by Selection object, that can be obtained as window.getSelection() or document.getSelection() . A ...
Read more >
SelectionState - Draft.js
getSelection() , which provides the SelectionState currently being rendered in the editor. Keys and Offsets#. A selection range has two points: ...
Read more >
Row Selection - JavaScript Data Grid
Getting the underlying node (rather than the data) is useful when working with ... and call the setSelected() method if it matches the...
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