Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting Number of Characters in a ContentEditable div

I am using contenteditable div elements in my web application and I am trying to come up with a solution to limit the amount of characters allowed in the area, and once the limit is hit, attempting to enter characters simply does nothing. This is what I have so far:

var content_id = 'editable_div';  //binding keyup/down events on the contenteditable div $('#'+content_id).keyup(function(){ check_charcount(content_id, max); }); $('#'+content_id).keydown(function(){ check_charcount(content_id, max); });  function check_charcount(content_id, max) {        if($('#'+content_id).text().length > max)     {         $('#'+content_id).text($('#'+content_id).text().substring(0, max));     } } 

This DOES limit the number of characters to the number specified by 'max', however once the area's text is set by the jquery .text() function the cursor resets itself to the beginning of the area. So if the user keeps on typing, the newly entered characters will be inserted at the beginning of the text and the last character of the text will be removed. So really, I just need some way to keep the cursor at the end of the contenteditable area's text.

like image 209
Bill Dami Avatar asked May 19 '10 16:05

Bill Dami


People also ask

How do I stop Enter key in Contenteditable?

To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed. to add a contenteditable div. document. addEventListener("keydown", (event) => { if (event.

How do you make a div Contenteditable?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

How do you focus on Contenteditable?

Changing your tabIndex to >= 0 will let you focus on the elements. If you need to do it dynamically, you can just add a tabindex >= 0 to your element in the event listener.

How do I limit a string in HTML?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.


1 Answers

How about passing in the event object to your function and calling e.preventDefault() if the max is reached?

var content_id = 'editable_div';    max = 10;  //binding keyup/down events on the contenteditable div $('#'+content_id).keyup(function(e){ check_charcount(content_id, max, e); }); $('#'+content_id).keydown(function(e){ check_charcount(content_id, max, e); });  function check_charcount(content_id, max, e) {        if(e.which != 8 && $('#'+content_id).text().length > max)     {        // $('#'+content_id).text($('#'+content_id).text().substring(0, max));        e.preventDefault();     } } 

Although, you may need to do a little more to allow the user to do things like 'delete'.


EDIT:

Added support for 'delete' key.


EDIT 2:

Also, you could probably get rid of the keyup handler. keydown should be enough.

like image 147
user113716 Avatar answered Sep 28 '22 03:09

user113716