Move the cursor to the end of a contentEditable element
Written byPhuoc Nguyen
Category
Level 1 — Basic
Created
18 Sep, 2023
At times, you may need to move the cursor to the end of a contenteditable element. This can come in handy when you want to focus on an empty element or add new content at the end.
For instance, imagine a chat application where users can reply to a message. It would be great if the cursor automatically moved to the end of the input field, which would already contain the original message, every time they start replying. This way, users don't have to manually move the cursor each time they want to type something new.
Another example is when you're building an editor. In this case, it's crucial that the cursor always stays at the end of the text so that users can continue typing without worrying about moving the cursor around. This can save them a lot of time and effort, and make their overall experience much smoother.
There are also situations where you might want to move the cursor to the end of an element programmatically. For example, if you have an input field with some text, but you want to add more text dynamically using JavaScript, you will need to move the cursor to the end of the input field before adding new text.
To move the cursor to the end of a contenteditable element, we can use a
`Range`
object. We simply set the start and end points of the range to be at the very end of our element, and then add this range to our selection using `window.getSelection().addRange()`
.Here's a breakdown of the steps involved:
const moveCursorToEnd = () => {
const range = document.createRange();
const selection = window.getSelection();
range.setStart(contentEle, contentEle.childNodes.length);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
};
To start, we create a new
`Range`
object by calling `document.createRange()`
. Then, we position the start of the range at the very end of our `contentEle`
node by setting its starting node as `contentEle`
and its offset as `contentEle.childNodes.length`
.Next, we collapse the range to ensure that only one point in our contenteditable element is selected. We do this by calling
`range.collapse(true)`
, which sets the endpoint equal to the start point.Finally, we remove any existing ranges from our selection by calling
`sel.removeAllRanges()`
. Then, we add our newly created range to the selection using `sel.addRange(range)`
.#See also
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn