Is it possible to "scroll up" when an editable element loses focus?

I want to create a contenteditable element that starts with a small width and expands as more content is added, eventually clipping the overflow. I also want this element to automatically scroll back to the beginning like a regular text box would.

Setting the caret position at the start of the content does not achieve the desired scrolling effect. Another approach I tried was clearing the content temporarily and then restoring it using setTimeout after 1ms, but this feels like a messy solution.

Using a textbox is not an option because I only want the content to be editable after a double-click event. Can someone please provide assistance?

Here is the HTML code:

<section contenteditable='true'>Edit me!</section>

And here is the CSS code:

section {
    background-color:#ccc;
    display:inline;
    white-space:nowrap;
    max-width:100px;
    position:absolute;
    overflow:hidden;
    padding:10px;
}

View the example here.

Answer №1

If you want to control the scroll position of an overflow, you can utilize the .scrollLeft property. I have implemented the 'onblur' attribute within your section element to trigger a function when the section loses focus, passing this as an argument for immediate use:

<section contenteditable='true' onblur="resetOverflowPosition(this)">This text should be sufficiently long enough to allow scrolling forward and having the script automatically scroll back</section>

In the function defined below, we set the scrollLeft property to 0 in order to reset the scroll position of the current section. This function can be called multiple times as needed:

function resetOverflowPosition(element){
    element.scrollLeft = 0;
}

For a demonstration, check out this JSFiddle link: https://jsfiddle.net/abcdefg123/. Feel free to reach out with any further questions or assistance.

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

Tips for creating a nested array in Vue.js in the correct way

In my Vue application, I am currently receiving a JSON object structured like this: [ { "meetingName":"ewq", "meetingUrl":"", "meetingPw":"", &qu ...

Substitute approach for Marionette driver action class

Previously, I had code to draw a rectangle in Selenium WebDriver which worked fine. However, after switching to Marionette driver, it stopped working because the Action class is not supported. I am looking for an alternative solution similar to using the R ...

Unable to navigate a simulated scrollbar

As someone who is new to web development, I am embarking on the journey of building a UI Grid that can effectively display a large amount of data. My goal is to implement a scrollbar that allows for horizontal scrolling across approximately 1,000,000 data ...

Does this sample Redux reducer from the latest Pro React 16 publication pass muster?

Currently, I am immersing myself in the world of React and Redux while delving into "Pro React 16" by Adam Freeman. In Chapter 5, there is an interesting reducer example that deals with actions related to a shopping cart. Here's a snippet: import { A ...

Javascript enables the magnetization of cursor movements

Can a web page be designed so that when users open it and hover their mouse over a specific area outside of an image, the mouse is attracted to the image as if by a magnet? Is this idea feasible? Any suggestions would be appreciated. ...

AngularJS - Enable user to edit dynamically calculated input

My addition calculator has a simple layout as shown below: <!DOCTYPE html> <html ng-app> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"> </script> </he ...

Exploring the World of Asynchronous Functions with Readline

I am trying to simultaneously use an async function and readline. However, I am encountering an issue where the code does not wait for the input in readline before moving on to the second part (.then). Despite my intention to see phoneCode in the console ...

Tips for inheriting method types from a parent class using JsDoc

I'm in need of writing JsDoc for an abstract method in the parent class so that it applies to all its sub-classes. Here's a basic example similar to what I'm dealing with: class Shape { /** Performs some complex functionality. * @p ...

How can I modify the HTML code for rooms in the HTML 5 client of the bigbluebutton application?

Is it possible to customize the HTML code of rooms within the BigBlueButton source code? Additionally, how can I modify the CSS styles of elements in room sessions? For instance, I am interested in hiding the logo of BigBlueButton by setting 'display: ...

Rearranging information within a JSON structure

Here is a sample of Javascript object and code to consider: Javascript Object { "thing":{ "data":"some data", "thumb":"some data", "data1":"some data", "data2":"some data", "data3":"some data", }, "extra1":[ ...

Utilize the AngularJS directive for enhanced functionality

In script A, I currently have this structure: module.exports = angular.module('myApp').controller(..).directive(..) I am looking to add an additional directive so that it looks something like this: module.exports = angular.module('myApp&a ...

Avoid importing the React CSS style file at all costs

Currently, I am working on a project and facing a minor issue related to importing the style.css file. I am aware that the syntax should be import "./style.css", however, upon implementation, I encountered a Terminal Output error. The output generated by ...

Deciphering WCAG 2.0 - the ins and outs of guideline 1.4.8

I'm really struggling to grasp the concept behind WCAG 2.0 criteria 1.4.8. As far as I can gather, it states that if I have a <div> where I set both the background and text colors, I must either: Include a color picker tool for users to custo ...

Navigating fluently in React Native applications

Struggling to grasp the proper implementation of navigation in RN? The provided code snippet should shed light on the current scenario. HeaderConnected is equipped with a menu button component that utilizes a custom navigate prop for opening the Drawer me ...

Retrieving information upon page loading and setting it as select options within a Vue JS environment

Currently, I am working on a straightforward form that includes a select type form. This specific form is initially created without any options as I intend to dynamically populate them from the backend later. Below is the code snippet for reference: < ...

Saving Backbone.js Collection as Text File on HDD & Re-importing Data

After experimenting with Backbone.js for a while, I've relied on localStorage to store most of my app data. However, I now want to explore the possibility of exporting my collection to plain text for easy backup purposes. Essentially, I envision a fea ...

Issue with Intel XDK: the document.location.href is directing to an incorrect page

Hello Community of Developers, I have been experimenting with different methods but still haven't found a solution. In my project using Intel XDK, whenever I attempt to change the page using location.location.href = "#EndGame" or similar codes in Java ...

The alignment of Div elements is off in IE8

I have been struggling to align two divs side by side without any empty spaces on IE 8. Everything looks perfect on Chrome Version 35.0.1916.153 m and Firefox 30.0, but IE 8 seems to be causing some issues. All I want is a main div with two child divs ali ...

Why aren't error messages showing up during Django login?

I’m in the process of creating a web application using Django with a customized login field. I want to integrate error messages for scenarios where the user does not exist or enters an incorrect password. Despite attempting various methods and researchin ...

Issue: A React component went into suspension during the rendering process, however, no alternative UI was designated

I'm currently experimenting with the code found at https://github.com/adarshpastakia/ant-extensions/tree/master/modules/searchbar Although I followed the tutorial instructions, I encountered an error. Could it be that the library is malfunctioning? I ...