How can we display the final section of the input text once it loses focus?

When inputting text, the right side of the text is hidden when we focus out or click outside the input field. I want to reverse this so that the left side is hidden and only the right side shows.

<input value="abcdefghijklmnopqrstwxyz123456789">

In the example above, we can see "abcdefghijklmnopqrstwxyz1" with the rest ("23456789") hidden. However, I want to display "klmnopqrstwxyz123456789" (the last part instead of the first part) and hide the initial part "abcdefghij" without using CSS direction.

Answer №1

By utilizing JavaScript, we can accomplish this task by making use of the blur event.

When the blur event is triggered, we capture the current Element.scrollLeft of the input field. Afterwards, we reset the scrollLeft position to Element.scrollWidth enclosed within a setTimeout() function to ensure that the browser delays rendering the pending change.

const elem = document.getElementById('data');

elem.addEventListener("blur", () => {
  setTimeout(() => {
    elem.scrollLeft = elem.scrollWidth;
  }, 0);
});
<input id="data" value="abcdefghijklmnopqrstwxyz123456789">

Answer №2

The following code snippet is designed to dynamically adjust the length of text based on its content:

function updateTextSize(input){
    input.size = input.value.length;
    console.log(input);
}

<input type="text" onkeyup="updateTextSize(this)" value="abcdefghijklmnopqrstwxyz123456789">

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 sanitize or replace the $& characters (dollar sign and ampersand) within a JavaScript string that cannot be escaped?

In relation to this particular question, I find that the provided solutions do not rectify my issue. The backend (Node.js) is receiving HTML content from the frontend (React): // Here's just a demonstration const price = '<strong>US$&n ...

Place an <a> hyperlink on top of an <img> element with adaptive responsiveness

I'm working on a React application and I want to overlay multiple links using <a> tags on top of an image <img>. Initially, this seemed easy but I ran into an issue - when the window is resized (on different devices, for example), the posi ...

Is it possible to pass a PHP array to JavaScript without using Ajax?

Currently, I have a JavaScript function that utilizes Ajax to fetch an array of data from PHP and dynamically populates a dropdown menu. Everything is functioning as expected. However, I am beginning to feel that using Ajax for this task might be a bit ex ...

Guide to sending a socket.io response in a post request using NodeJS

I've got a node server up and running with a React application that's integrated with Socket.io, and everything is working smoothly. However, I also have an external application that relies on the same data. My goal is to post to my node server w ...

Is it risky to replace the default 'console' in node with something else?

As a creature driven by habits, I find myself instinctively using console.log/console.error/etc to print things out. However, in an effort to centralize my logging in node, I am transitioning to 'winston'. My plan is to include var console = requ ...

The $(document).ready() function triggers as soon as the document is fully

I created a simple webpage with the following HTML: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>hi</title> <script type="text/javascript" src="js/jquery-1.8.2.js"></script> <link type="t ...

What is the best way to unselect a checkbox that is already checked on an HTML page using AngularJS?

I've created checkboxes in my HTML code like this: <div ng-repeat="fruit in fruits"> <input type="checkbox" name="{{fruit.name}} ng-model="fruit.value" ng-checked="isChecked(fruit)" ng-change="changed(fruit)" ng-required="true" ...

What is the best way to retrieve a particular nested value from a fetch POST response?

I performed a POST request in this manner: var myHeaders = new fetch.Headers(); raw = "srvrwd-ui=useMe" var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: &a ...

The occurrence of the "contextmenu" event can cause disruption to the execution of a function triggered by the "onmousemove" event

Currently in the process of developing a Vue application utilizing a Pinia store system. Within my BoxView.vue component, I have created a grid layout with draggable elements that have flip functionality implemented within the BoxItem.vue component. Spec ...

Unsure about how to handle JavaScript and mechanize in this particular situation

My task involves accessing multiple accounts on Amazon's KDP platform, which can be found at To login to each account and check their earnings, I rely on Mechanize for handling the cookies. However, the page displaying the earnings uses Javascript to ...

How to insert an image into a table using FPDF in PHP

I am working on some HTML: $html='<table width="100%" class="p" id="tblExport"><tbody> <tr><th>#</th><th>product</th><th>picture</th><th>product_category</th><th>avl_qty</ ...

Transferring data between components: Comparing Passing References and Using Subjects

Question: Is passing variables by reference between Angular components a better option than using BehaviorSubjects? I'm currently developing a diary application in Angular 8 with two components (Navbar and Dashboard) and a service (EntryService). Th ...

What is preventing me from setting the moment locale in node.js?

I have a node-app running, and the contents of my app.js file are as follows: var moment = require('moment'); moment().locale('fr'); console.log(moment.locale()) Despite expecting the output to be 'fr', it actually displays ...

problem | JQuery slide causing automatic page scroll to top

Here is the HTML for my slide: <div class="slide_container"> <div class="one_slide"> <a href="#"><img alt="slide_img" class="slide_img img-responsive" src="images/slide2.jpg"></a> </div> ...

Switching picture using dropdown menu in javascript/jquery

Having some trouble trying to recreate this example. The initial image shows up, but it doesn't change when I select a different option. Still getting the hang of javascript so any help is appreciated. <html> <head> <script src="/j ...

Using jQuery to vertically center a div

When a vertical menu on my website is clicked, it pops out from the left side of the screen. The content inside is vertically centered using CSS transformations. While expanding to show sub-items, everything remains centered. However, there's an issue ...

Optimizing workflow with express.js, backbone.js, and connect-assets for maximum efficiency

As a newcomer to node.js, I'm embarking on the challenge of setting up an application that utilizes Backbone.js on the client-side while being supported by express.js and node.js for server-side extensibility. The lack of online examples showcasing a ...

Accessing a precise div element in a webpage

Currently, I am utilizing Vue.js and Nuxt for my web development. One issue I am facing is related to anchors. Specifically, when I navigate to mysite.com/page1#section1, I want the page to scroll to section1. In my code, I have the following snippet: < ...

Increasing the token size in the Metaplex Auction House CLI for selling items

Utilizing the Metaplex Auction House CLI (ah-cli) latest version (commit 472973f2437ecd9cd0e730254ecdbd1e8fbbd953 from May 27 12:54:11 2022) has posed a limitation where it only allows the use of --token-size 1 and does not permit the creation of auction s ...

`Incorporating width and vertical alignment in an image`

I am trying to figure out a way to make the image fill up 100% of the width, aligning it vertically with the text below. The code I'm working on is for email newsletters using foundation. Check out my Example Site Unfortunately, I can't seem to ...