Using a contenteditable div with a set maximum character limit, powered by jQuery

Here is the challenge at hand - I am looking to restrict input to no more than N characters in a contenteditable div using either native JS or JQuery. For example, if a user inputs 10 symbols, they should not be able to input any more and can only clear the input.

<div contenteditable="true" placeholder="Message Text" class="editable ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"></div>

Answer №1

If you need a solution, this should do the trick. Visit the fiddle here

$('#editcontent').on('keydown', function(event) {
  $('span').text('Characters entered:' + $(this).text().length);
  if ($(this).text().length === 10 && event.keyCode != 8) /*delete*/ {
    event.preventDefault();
  }
});
body {
  padding: 10px 0 0 10px;
}
#editcontent {
  width: 200px;
  height: 50px;
  border: 1px solid blue;
}
#editcontent:after {
  color: #999;
  content: 'Enter text';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="editcontent" contenteditable="true"></div>
<span></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to organize arrays with various combinations?

I am trying to rearrange the elements in 3 arrays: ["s","m"], ["Red","Black"], ["1", "2"]. My desired arrangement is as follows: ["s","Red","1"], ["s"," ...

Bootstrap - extra spacing

Utilizing bootstrap, I have implemented two div elements in my HTML code: <div class="container"> <div class="col-12 circle lg-circle"> <div class="col-12 circle sm-circle"> </div> </div> </div> The corresp ...

The isolate scope variable is becoming undefined within the link function

When working with a directive that takes a data object and a function in its isolate scope, I encountered an issue. Inside the link function, I declared a method to be triggered on a button click event. The problem is that while the value passed to the me ...

Error: Attempting to access the 'street' property of an undefined value

My application loads a JSONPlaceholder data list of users, and I am attempting to enable editing of this data. However, I encounter an error: TypeError: Cannot read property 'street' of undefined Is there anyone who can provide assistance? c ...

What is the best way to create an animation for a dynamic menu background image

While hovering over a list item, I want to create an animation where the background image appears to zoom out. $(function () { var image = $('.menu').find('img').attr('src'); $('.menu ul li').mouseover(fun ...

Unable to Load Complete Calendar

I'm having trouble getting my full calendar to appear. Can anyone help me with this issue? See the error below. https://i.sstatic.net/b5QG8.jpg ...

Utilizing various camera set-ups within Three.js

How can I smoothly switch between two different cameras in Three.js while transitioning from one to the other? Imagine a scene with a rock and a tree, each having its own dedicated camera setup. I'm looking for a way to seamlessly transition between ...

Retrieve the value of the callback function from outside the callback function (ajax)

My dilemma involves returning a callback value outside of the callback function. Here's an example: I created this function based on the topic discussed in: How do I return the response from an asynchronous call? (function (){ return getAjaxRes ...

Rectangles in collision: A mathematical analysis

After numerous attempts, I have developed a small "game" that incorporates collision detection. Unfortunately, I have encountered a persistent issue where objects sometimes pass through each other. The root cause of this problem eludes me completely. Ini ...

Update input field value with data retrieved via ajax call in AngularJS

My current approach involves using AngularJS directives for Bootstrap in order to create an edit form on a Bootstrap modal when the user clicks on the edit button from a list of items. Here is the code I have implemented: HTML: <div class="modal-heade ...

Transforming the JSON structure in JavaScript

Is there a way to transform the JSON format below into the desired output? [ { "type": "System Usability Score", "score": 74, "date": "2020-03-19T18:30:00.000+0000" }, { "type&quo ...

Saving HTML Table Data to MS Access

Currently, I am attempting to import the table from the link below into my database: This involves downloading the page as an HTML file through VBA and then creating a linked table. Although the manual download via Chrome works perfectly using the "Downl ...

Shut down the popup window

I am diving into the world of jQuery and have successfully created a popup window with a button. However, I am encountering an issue where clicking on the button does not close the popup window as intended. Any guidance or assistance would be greatly app ...

Adjusting the text color of ul items with CSS

I currently have a menu setup like this: <ul class="ipro_menu"> <li><a href="/">Home</a></li> <li class="active-parent"><a href="/menu-item-1/">Menu Item 1</a> <ul class="sub-menu"> ...

Is client-side rendering the new trend, bypassing traditional server-side rendering?

I'm exploring the idea of rendering my pages on the client side to reduce server load and minimize traffic. Typically, the first request to the server requires it to render the requested page and then send it to the user. Once the user interacts with ...

What is the best way to cache node_modules when package.json remains unchanged during yarn or npm install within a Docker build process?

A Dockerfile for a node application is structured as follows: FROM node:8.3 ENV TERM=xterm-color NPM_CONFIG_LOGLEVEL=warn PATH="$PATH:/usr/src/app/node_modules/.bin/" WORKDIR /usr/src/app ADD . /usr/src/app RUN yarn install --frozen-lockfile --ignore-plat ...

Having difficulty configuring the new Faker library

I've been working on setting up the brand new @faker-js/faker library. Here's what I have done so far: npm i @faker-js/faker -D I added faker.d.ts at the top level of the tree, so it looks like this: https://i.sstatic.net/Hkzh8.png The content ...

What is the best way to refresh resources after the state of the Admin component has been updated?

My approach to dynamically obtaining resources through a function was working well until I encountered an issue with changing screen sizes. I believed that by adding a state called "screenSize" in the admin component and updating it upon resize, I would ...

Prevent Repetition of Meta Descriptions and Keywords within Next.js

I'm currently working on my website using Next.js. In order to enhance the SEO performance of my site, I am making an effort to steer clear of duplicate meta tags. My Inquiry According to Next's official documentation, they suggest that using t ...

What is the correct way to properly define the height of a page containing an angular-split area?

I am currently working on a page that utilizes angular-split. Here is a snippet of the code: <div class="height-max"> <app-nav-menu></app-nav-menu> <as-split direction="horizontal"> <as-split-area> <route ...