CHIM
CHIM

Reputation: 35

set a limited length to element with contentEditable in Javascript

I have a html <div id = "a"></div> and js code:

const a = document.getElementById("a");
a.contentEditable = true;

and I want to set a max length of 30 to the a like input text. I've tried using

a.addEventListener("input",() => {
        if(a.innerText.length == 30) {
            a.contentEditable = false;
    }

but the outcome is not satisfing. It did stop the further input but also stoped me to edit a.

Upvotes: 0

Views: 109

Answers (1)

CHIM
CHIM

Reputation: 35

I got the desired result by adding

a.innerText = a.innerText.slice(0,30);

Upvotes: 1

Related Questions