When contenteditable divs are reloaded, the hidden content disappears

When it comes to editing text, I utilize contenteditable divs on my website. The height of the div dynamically grows as I make changes, causing all other elements below it to shift downwards - which is exactly what I want. Once I've finished editing, I store the content on a server using Ajax and everything functions smoothly.

The issue arises when I reload the content from the server - the height of the contenteditable divs does not automatically adjust to display all of its content. Instead, it remains at its initial size.

This problem persists across all browsers. Does anyone have suggestions on how to address this issue?

EDIT

Here is the relevant HTML and JavaScript code for handling contenteditable:

<div class="editable" contentEditable=true onkeyup="autoGrowSave(this)" onblur="update(this)"></div>

function autoGrowSave (oField) {
  if (oField.scrollHeight > oField.clientHeight) {
    oField.style.height = oField.scrollHeight + "px";
  }
}

I use the above JavaScript function to enable automatic growth of the contenteditable. Though, I'm uncertain if it's even needed, as the jsFiddle example provided by Abhitalks doesn't require one. Perhaps I should attempt removing it...

To save content to a server, I utilize the onblur attribute, which functions flawlessly.

Answer №1

After some investigation, I managed to solve the issue on my own. Shoutout to Abhitaks jsFiddle for pointing me in the right direction and helping me identify the root cause of the problem. Much appreciated!

The culprit turned out to be a CSS rule where I had set an initial height of 20px for the class editable. This was preventing the contenteditable from adjusting its height dynamically. To work around this limitation, I resorted to using a JavaScript function called autoGrow(). While it did the trick during content editing, it failed to maintain the desired functionality upon page reload.

The ultimate fix involved removing the height property from the CSS rule altogether and doing away with the autoGrow() function. Voila! Problem solved without any unnecessary workarounds.

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

Is there a way to specify the dimensions of the canvas in my image editing software based on the user-input

Here is the code and link for my unique image editor. I would like to allow users to enter the desired width and height of the selection area. For example, if a user enters a width of 300 and height of 200, the selection size will adjust accordingly. Addit ...

Encountering difficulties including Bootstrap in React-app after attempting global installation

After using the command npm i -g bootstrap to install bootstrap, I attempted to include it in a React-app by adding import "bootstrap/dist/css/bootstrap.css";. Unfortunately, this resulted in an error being thrown. Failed to compile. ./src/index.js Modu ...

Current selection in a dynamic menu within an Express universe

In my Express.js app, I am working on creating a simple menu. My goal is to add the "active" class to the menu list item of the page that is currently being visited. Here is the code snippet from my Jade view file: li(class = path === "/id/admin" ? "activ ...

Accordion A and B both require the first category of accordion A to be opened first in order for the first category of accordion B to also be

Greetings, this is my initial inquiry, please excuse any errors as English is not my first language. For my project, I am utilizing Angular 6, jQuery, and BS. I have two accordions, each with different categories: A1 and A2. My goal is for clicking on A ...

How can I use jQuery to add styling to my menu options?

Looking to customize my menu items: <ul> <li class="static"> <a class="static menu-item" href="/mySites/AboutUs">About Us</a> </li> <li class="static"> <a class="static-menu-item" href="/m ...

Implementing method overrides in TypeScript class objects inherited from JavaScript function-based classes

I am facing a challenge with overriding an object method defined in a JavaScript (ES5) function-based class: var JSClass = function() { this.start = function() { console.log('JSClass.start()'); } } When I call the start() method, it pri ...

What is the best way to toggle between CSS rules on the left and right using jQuery?

I've incorporated a CSS class by using the following code: .dropdown-menu{right:0;} However, now I want to change the CSS property on the client side to: .dropdown-menu{left:0;} ...

What is the most effective method for creating unit testing functions in JavaScript?

When it comes to JavaScript, there are multiple ways to write the same functions. For example, consider the following options. Which approach is ideal for unit testing scenarios? // Option 1 ============ var app = {}; app.name = "abc" app.init = funct ...

Exploring Client-Side Routing using Angular within a Node.js and Express environment

I am currently using Node and Express on the server side, along with Angular on the client side. However, I am facing difficulties implementing Angular Client-Side routing. Below is a snippet of my Angular router configuration: app.config(['$routePr ...

ng-repeat: generate select options from an array

My JavaScript object is quite simple and looks like this: $scope.obj = { "'Architect'": ["asdf","d","e","y"]}; I am trying to display the values of 'Architect' in a select box. The issue arises because of the single quotes, making it ...

What causes the preview pane in an embedded Stackblitz project to not show up?

I've been attempting to integrate Stackblitz projects into my Angular application. The project editor pane displays correctly, but the preview pane is not showing the preview. Instead, it's showing an error message that reads: Error occurred: Err ...

Error: Primefaces ajax status failure (dialog has not been defined)

I incorporated the same code found on primefaces.org, specifically this link: http://www.primefaces.org/showcase/ui/ajaxStatusScript.jsf <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/> <p:dialog modal="true" ...

Retrieve information from a webpage with webharvest

I'm attempting to scrape all the HTML pages from the website "http://www.tecomdirectory.com/" using webharvest. However, the script isn't successfully fetching all the HTML pages and is only able to scrape a few of them. Below is the script that ...

Ensure the accuracy of submitted form information

I am seeking to enhance the validation process for my form, which is already using jquery.validate.min.js for validation. I want to incorporate another layer of validation by making an ajax call to my MySQL database to check if the email address provided i ...

Is there a way to utilize a Javascript function to incorporate commas into a div tag?

Being new to JavaScript, I have heard about jQuery but I don't know how to use it. Please bear with me as I am not well-versed in this area. :) I am currently importing data dynamically from an Excel database to a website. Everything is working fine ...

What is the best way to clear and refresh specific query parameters from the current URL?

For instance, my current URL looks like this: http://domain.com/category/health-beauty/?session={%22session_key%22%3A%22df1eca3f79e122bd915651e5-1325102496%22%2C%22uid%22%3A%221325102496%22%2C%22expires%22%3A0%2C%22secret%22%3A%229eb858d1fc7dda2b37be91228 ...

Having trouble updating a variable's state with setState on click

Struggling with a consistent issue while working on a React project. The problem lies in setting a variable into state using setState upon a click event. Specifically, I am facing difficulty passing my player object into state as ActivePlayer when the co ...

Arrange array with objects in order of most recent date

I am in the process of developing a chat application where all contacts are stored as objects in one array: var contacts = [{name: "Ruud", age: 20},{name: "Elke", age: 17}]; Next, I have the chat messages for each contact saved in an array of objects: v ...

Attempting to create a responsive Boxplot in Three.js

I created a basic three.js boxplot that loads individual jpg images for each mesh (bar). The boxplot appears fine on desktop, but is stretched/distorted on mobile devices. Is there a recommended approach to make it responsive? For simplicity, I will use e ...

Is it possible for HTML to call library scripts in the sidebar?

I recently encountered an issue with a sidebar created in HTML on Spreadsheets. Everything was working perfectly until I moved the code to the main library where all the code is managed. Now, it seems that the function used by the sidebar is undefined. How ...