The drawbacks of using ContentEditable in Firefox

My current setup involves using a div element with the contenteditable attribute. On keypress, I have restricted editing to 15 characters. When the focus is lost, the div becomes uneditable. Below is the code snippet that I am working with:

<div id="test" contenteditable="{{isContentEditable}}" onkeypress="return (this.innerHTML.length <= 15)"
ng-blur="handleEditableBlur();" ng-keydown="handleKeydown($event)"> {{value}}                    
</div>
<span id="pencilIcon" class="fa fa-pencil" data-ng-click="handleEdit();" focus="test"></span>

After clicking the pencil icon, the contenteditable property is set to true. Once the text exceeds 15 characters, it becomes uneditable again. While this functionality works perfectly in Chrome, there seems to be an issue with Firefox.

Answer №1

Applying a clever workaround, I have modified certain keyboard keystrokes like tab, backspace, delete, etc., to bypass the logic and return false. Afterwards, you can track the number of characters entered.

To implement this in your div, make sure to include

onkeypress="return checkEntry(event)"
and add the JavaScript function provided below to your JS script.

function checkEntry(e){
  e = e || window.event;
  var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
  var keyShorts = $.inArray(keyCode,new Array(8,9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,45,46,123,144,145))>-1;
  if(keyShorts){
    return true;
  }else if(e.target.innerHTML.replace(/(<|&lt;)br\s*\/*(>|&gt;)|(&nbsp;)/igm,' ').length>=15){
    return false;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo" style="width:300px;height:300px;border:1px solid #ccc;" contenteditable=true onkeypress="return checkEntry(event)"></div>

Feel free to utilize this solution for your needs. Cheers!

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

Enhancing an image with zoom effect in a 2-column layout on hover

Could someone help me achieve a 2-column layout where the left column contains text and the right column an image? I want the image in the right column to zoom when the user hovers over either the left or right column. The issue is that when the user hove ...

Notify the user with a message that our support is limited to Chrome, Firefox, and Edge browsers when utilizing Angular

How can I display a message stating that we only support Chrome, Safari, Firefox, and Edge browsers conditionally for users accessing our site from other browsers like Opera using Angular 10? Does anyone have a code snippet to help me achieve this? I atte ...

In an HTML form, there are two fields to input registration number and full name. I need a script in JavaScript that automatically fills in the full name field when the corresponding registration number is entered

<html> <head> <title>Registration Form</title> </head> <body> <form> <!--at runtime, fill in the full name when the user enters a registration number associated with their name--> Registration ...

Is there a way to upload an image without utilizing or concealing the `<input type='file' />` element?

Currently, I am developing a PHP script that retrieves the directories and files from specified local computer drives. The goal I am striving for is to enable users to upload an image to my server by clicking on its name from the listing, without using th ...

The presence of inline display causes gaps of white space

Located at the bottom of this webpage is a media gallery. The initial three images comprise the photo gallery, followed by video thumbnails inlined afterwards. However, there seems to be an issue with the alignment of each element within the video gallery. ...

Discover the inner workings of the code below: set a variable called "start" to the current time using the new Date().getTime() method. Create a loop that continuously checks if

I'm having trouble understanding how this code snippet works. Can someone please provide a demonstration? Thanks in advance.... It seems that the code is subtracting 'start' from the current date with new Date, resulting in 0 which is less t ...

What is the best way to compare input to 2 distinct patterns in order to produce 2 separate error messages accordingly?

Is there a way to use HTML and angular to validate input against two patterns and generate two different inline error messages? The "pattern" attribute in the input tag doesn't seem to work for this. For instance, I need to validate if the input star ...

"Using jQuery to add emphasis to a table row and remove it again

I have a table with multiple rows, where I have styled each odd row in one shade of gray and the even rows in slightly different shades to make it easier to read. When clicking on a row, I am currently highlighting it with a different color to indicate wh ...

Transition smoothly between images using CSS in a continuous loop

Is it possible to create a looped fade effect between images using only CSS, without the use of JavaScript? I attempted to achieve this by utilizing keyframes but was unable to succeed. Any guidance or assistance would be greatly appreciated. Thank you! ...

The relationship between columns in MySQL is determined by the values they hold

I have 2 tables with the following structure: mysql> select * from customer; +-------+-------+ | name | code | +-------+-------+ | John | c1 | | Mary | c2 | | Sarah | c3 | +-------+-------+ mysql> select * from supplier; +-------+----- ...

Issues arise when using jQuery's ajax() method with processData set to false, as it still adds form data to the URL, among

Today, I am delving into the world of jQuery to enhance a form's functionality, particularly to interact with a MySQL database. Here is the form I am working with: <form class="loginform" id="loginform" name="loginform"> <input type="ema ...

Handling the scope object within an Angular directive

Presently, here is my element directive: app.directive("ngArticle", [function(){ return { restrict: "E", replace: true, templateUrl: "/partials/article.html", scope: { article: "=article" } } }]); It is utilized as follows: <ng-ar ...

Consistent column height across each row

Currently, I am utilizing bootstrap and jquery to accomplish a specific task. My goal is to select the first child class after all columns on the webpage. var one = $(".col-x .some-class") Afterwards, I aim to obtain the height of all elements with the . ...

Can importing a library generate a fresh copy of the imported library?

In my development setup using webpack and vue-loader to build a Vue.js application, I have various .vue files for different components. Whenever I include the line: import _ from 'lodash' in the script section of both ComponentA.vue and Compone ...

A fleeting moment reveals a broken image tag

How can I prevent the broken image tag from briefly appearing when the page loads? Take a look at this example: http://jsfiddle.net/v8DLe/196/ I have already implemented: onerror="this.style.display ='none'" Is there a way to still use the img ...

Restrict the quantity of items retrieved from an AJAX response

An AJAX call has returned a response consisting of a list of <a> elements: <a href="/1/">One</a> <a href="/2/">Two</a> <a href="/3/">Three</a> I am looking to select only the first n a elements from this response ...

Interacting with Rails controllers through AJAX to trigger a JavaScript function

After exploring numerous stack overflow posts without finding a working solution, I decided to seek help for a well-documented use case. I have a button that should perform the following tasks: When clicked, call a custom controller method to update th ...

I find it frustrating that Angular consistently redirects me to the login page every time I attempt to navigate to a different page

I currently have a website with both normal user-accessible pages and an admin dashboard accessible only by admins through ngx-admin. To restrict users from accessing the admin dashboard, I've implemented an auth guard that redirects them to the logi ...

How can I show the initial three digits and last three digits when using ngFor loop in Angular?

Greetings! I have a list of numbers as shown below: array = [1,2,3,4,5,6,7,8,9,10] By using *ngFor, I am displaying the numbers like this: <div *ngFor =" let data of array"> <p>{{data}}</p> </div> Now, instead of d ...

What is the best way to give a fixed height to Card content in Material UI? Is CSS the way to go?

I've been struggling with a design issue involving Material-UI cards used to display News. The problem arises when the paragraph section of the card occupies multiple lines of text. When it spans two lines, there is a 10px spacing between the paragrap ...