When div is set to contentEditable=true, the cursor cannot be positioned after resetting its content

When div is set to contentEditable=true, the cursor cannot be positioned after resetting its content

I was recently working on a comment feature that required the ability to comment on emoticons, so the contentEditable property was the first thing to consider. As a result, a problem arose...

First, comment on the district chief:

img

When the input content exceeds the limit, clear the content after the user input exceeds the limit.

This is easy... But after clearing the content, the cursor actually moved to the front, which is a headache.

After various searches, I found a solution. Without further ado, here is the code!

var _div = document.querySelector('.discuss_area');
var range = document.createRange();

range.selectNodeContents(_div);
range.collapse(false);

var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

All of them use native events, so jQuery objects cannot be used.

This article is about solving the problem that when a div is set to contentEditable=true, the cursor cannot be positioned correctly after resetting its content. This is the end of the article. For more relevant content about div setting contentEditable=true, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Common naming rules for CSS classes and ids

>>:  Detailed explanation of CSS background and border tag examples

Recommend

Summary of Common Letters in Unicode

Most of the earliest computers could only use ASC...

VMware WorkStation 14 pro installation Ubuntu 17.04 tutorial

This article records the specific method of insta...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Pitfalls and solutions encountered in MySQL timestamp comparison query

Table of contents Pitfalls encountered in timesta...

How to implement vertical text alignment with CSS (Summary)

The default arrangement of text in HTML is horizo...

Summary of mysqladmin daily management commands under MySQL (must read)

The usage format of the mysqladmin tool is: mysql...

Nodejs error handling process record

This article takes the connection error ECONNREFU...

Vue custom optional time calendar component

This article example shares the specific code of ...

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...

Using radial gradient in CSS to achieve card effect

A few days ago, a colleague received a points mal...

The best solution for resetting the root password of MySQL 8.0.23

This method was edited on February 7, 2021. The v...

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...

Two simple ways to remove text watermarks from web pages

<br /> When we browse certain websites and s...

JS implements the sample code of decimal conversion to hexadecimal

Preface When we write code, we occasionally encou...