Charlimit in contenteditable

In HTML, you can make elements editable by setting contenteditable="true". Is there a native charlimit attribute, or a non-JS way to set the content limit to 1 character?

I’m not 100% sure, but you can’t.

HOWEVER, you can achieve this using the innerHTML JavaScript attribute

:thinking: There is maxlength

That’s only for textareas and input elements. Shouldn’t work.

oh, that’s right :thinking:


or

When you add contenteditable="true" to an element, whatever contents you’re adding in the element becomes a child element of that element.

For example, when you type some text into a div, the text becomes a <p> tag under the div. So you could use JavaScript to get the innerText of the element, and make sure it has only 1 character.

2 Likes

I did not know that it would create a p tag

content editable does let you delete the child elements

try dragging this to your bookmarks space and test it in google.com or some other site.

Didn’t know that - gotta test it out

1 Like