After reading the articles about creating your own WYSIWYG editors for admin panels, I assumed that for JavaScript there simply is no carriage inside the input fields, so it cannot work with it and solving such seemingly elementary tasks as carriage surrounding than -or the movement of the carriage to the desired position, registration of the current position of the carriage, etc., is achieved in rolls of unreadable code. Is it true that JavaScript does not see the caret in the browser?


Update (moved from comment)

Information from all the discussions in the comments will certainly come in handy, but I will return to the main topic. I will ask this: here is a cursor inside the input field and there is javascript. What is the reason why in native JavaScript there is no means to work with a carriage such as getCaretPosition() , moveCaretToPosition() , getTextAfterCaret() , etc.? Surely this is not laziness of developers, but some difficulties of JavaScript interaction with the browser.

  • And what for you "carriage in the browser"? Do you mean the caret in the input fields of the DOM? You can go further, javascript не видит никакого браузера - vp_arth
  • The carriage is a typewriter. There is a cursor location. And it is a property of a specific control (say, a textbox), and accordingly for each control its value. - Akina
  • one
    @Akina, Simply the word "cursor" is used with respect to both the mouse pointer and the pointer inside the field. But with the terminology okay. Could you give an example of how to refer to the corresponding properties of textarea? - Gleb
  • @vp_arth, so for sure, the caret is inside the input fields of the DOM. - Gleb
  • one

1 answer 1

The Selection object represents a range of text selected by the user or the current position of the cursor (caret).

This object is not part of JavaScript, it is implemented by the browser.

The following fields are available to control the cursor in the TextArea :

  • selectionStart Returns / sets the index of the beginning of the selected text. If there is no selected text, returns the index of the character following the cursor.
  • selectionEnd Returns / sets the index of the end of the selected text. If there is no selected text, returns the index of the character following the cursor.
  • one
    Thank you for your reply! Please tell me what it means "is not part of JavaScript, but is implemented by the browser"? If this object is, then it should be somewhere represented in any language, and if not in JavaScript, then in which one? - Bokov Gleb
  • JavaScript is a language described by a set of ECMA standards, each browser has its own implementation of a language and runtime environment. Selection - part of the runtime in the browser. For example, it is not in the JavaScript implementation in node.js or older versions of popular browsers. - Ivan Solntsev