title: Limiting the number of characters in a ContentEditable div
body: <p>I am using contenteditable <em>div</em> 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.</p>
  <p>This is what I have so far:</p>
  <pre><code>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 &gt; max)
      {
          $('#' + content_id).text($('#' + content_id).text().substring(0, max));
      }
  }
  </code></pre>
  <p>This <em>does</em> 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.</p>
  <p>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.</p>