What is the secret to ensuring a contenteditable div only accepts plain text?

One issue I have encountered is that when copying and pasting styled text into a div textarea, the style remains attached to the text. Although setting contenteditable='plaintext-only' can resolve this problem in Webkit browsers, it does not work in other browsers.

Is there a way to restrict a div to only allow plain text input?

If you need a point of reference, check out this link: https://jsfiddle.net/w25dnuen/

Answer №1

Check out this demonstration of a text area that expands based on the content you input:

http://jsfiddle.net/janjarfalk/r3Ekw/

The functionality of the elastic text area is made possible by an external script called jquery.elastic.source.js, which can be found here:

Now that you have explored these resources, let's delve into how it works. The script includes an "update" function that simply adds whitespace when reaching the end of a row like so:

// Add an extra white space so new rows are added when you are at the end of a row.
$twin.html(textareaContent+' ');

Additionally, there are specific functions for setting the width and height of the text area dynamically:

// Updates the width of the twin. (solution for textareas with widths in percent)
function setTwinWidth(){
    var curatedWidth = Math.floor(parseInt($textarea.width(),10));
    if($twin.width() !== curatedWidth){
        $twin.css({'width': curatedWidth + 'px'});
    
        // Update height of textarea
        update(true);
    }
}

// Sets a given height and overflow state on the textarea
function setHeightAndOverflow(height, overflow){

    var curratedHeight = Math.floor(parseInt(height,10));
    if($textarea.height() !== curratedHeight){
        $textarea.css({'height': curratedHeight + 'px','overflow':overflow});
    }
}

All of this functionality is part of the elastic feature. Hopefully, this breakdown sheds some light and proves useful to you.

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

A guide on how to apply HTML attributes like checked, selected, and disabled in jQuery without providing values

This Question May Have Been Asked Before: Set attribute without value At times, when working with HTML, there is a need to assign an attribute to a DOM element without providing any specific value. For example, look at the selected parameter in this s ...

Excessive screen height causes the CSS box layout to overflow

I am looking to create a screen layout similar to the one shown in this image. It should consist of a row with no overflow and include: A left box that contains: A fixed height header (20px), An aspect square box taking up the remaining height. ...

manipulating session variables with javascript ajax and php

I'm struggling with setting and retrieving session variables using JavaScript code that calls PHP functions via AJAX. I want to access the returned session field in my JavaScript, but nothing seems to be working. Can someone take a look at my code and ...

A guide on how to align Hamburger CSS in the center, center text, and include a

For a while now, I've been attempting to achieve three specific things with a Hamburger menu, but so far, I've had no luck. This Hamburger menu is responsive, transitioning into an X symbol upon clicking and moving to the right of the page with a ...

Transferring information between sibling modules in Angular 2

In my Angular 2 project, I have a parent component with two sibling children components. Sibling1 has a button, and Sibling2 has an h1 element that should display text when the button in Sibling1 is clicked. I've been advised to use a shared service t ...

Attempting to filter out any parameter from the GET request that does not have a value assigned

I've coded the following section in my search.html: <h1>Property Search</h1> <hr> <form method="GET" action="/search/results/?{{ query_params }}" class="form-inline" style="display: flex; ...

What is the recommended Vue js lifecycle method for initializing the materialize dropdown menu?

https://i.stack.imgur.com/cjGvh.png Upon examining the materialize documentation, it is evident that implementing this on a basic HTML file is straightforward: simply paste the HTML code into the body and add the JavaScript initializer within a script tag ...

What could be causing the JSON.parse() function to fail in my program?

Currently utilizing Django and attempting to fetch data directly using javascript. Below are the code snippets. Within the idx_map.html, the JS section appears as follows: var act = '{{ activities_json }}'; document.getElementById("json") ...

Implementing the .load function to dynamically load dynamically generated content

I've been exploring the combination of "dynamic" and .load() for some time now, but every time I try to mix those words together, I seem to get unexpected (or expected) results. Let me explain clearly. Let's say I have two pages, one is the inde ...

Updating the ajax script from 1.8.3 jquery to version 1.9

I am facing an issue with a pagination script that requires jQuery ver. 1.8.3 while my website is using version 1.9.1. Being new to jQuery, I am struggling to figure out how to resolve this. It seems like there might be a syntax problem with ajax and Parse ...

Utilize the get method to showcase data in a material UI table for a React application

Just starting out with React. Managed to create a table component with hard-coded data. However, I now have all the data stored in table.json. Can someone guide me on how to fetch values from table.json using an axios get request an ...

Styling Your HTML Background with CSS and Enhancing it with JavaScript

I am lost when it comes to understanding the design of I have spent hours researching how to create backgrounds using shapes and css, but I haven't been successful. Can someone please help me figure out how to create a background similar to . Thank ...

The functionality of ng-disabled in AngularJS is not functioning properly

This form is not behaving as expected. <form name="myForm"> <input ng-disabled="true" type="text" >* </form> I can't figure out why the input field is not disabled as intended. I even tried using $scope in the controller: $scope.d ...

Adjusting body styles in a NextJS application using localStorage prior to hydration: A step-by-step guide

If you visit the React website and toggle the theme, your choice will be saved in localStorage. Upon refreshing the page, the theme remains persistent. In my NextJS project, I am attempting to achieve a similar outcome by applying styles to the body eleme ...

Select the initial element from a group of elements with a specific attribute using CSS

Imagine we are faced with the following code snippet: <node> <element bla="1"/> <element bla="2"/> <element bla="3"/> <element bla="3"/> <element bla="3"/> <element bla="4"/> </node> How can we ...

What is the best way to horizontally center my HTML tables and ensure that they all have uniform widths?

I'm currently facing an issue where I'd like to center align all tables similar to the 'Ruby Table.' However, I'm unsure of how to achieve this. You may also observe that some tables have varying widths. What CSS rule can I apply ...

Incorrect value for `this` is displayed in TypeScript by Visual Studio

Take a look at the code snippet below: class Person{ firstname = ko.observable<string>(); lastname: ko.observable<string>(); fullname = ko.computed(()=>{ // Breakpoint here return this.firstname() + ' &apos ...

What is the best way to convert this to JSON format?

Is there a way to send multiple values via JSON for retrieval in a similar manner? foreach($_POST['listItem'] as $key => $values){ list($eventDate, $eventTitle) = $values; } Currently, the values are being sent like this but it seems t ...

Challenges with JavaScript Object and Async forEach Loop - issues in assigning values

My intention is to create an object called 'tallys' that contains a key for each user in a game. The value assigned to these user keys is an array representing the weekly dollar values won in the game. Initially, I allocate each user in the game ...

Having trouble setting background images for CSS ID, but it's working perfectly for CSS Body

When working with my .CSS file: Initially, applying the following styling: body { font-size: .85em; font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif; color: #232323; background-color: #fff; background: url('Images/Login_Background.jpg'); ...