Display the final character of the password input field that can be edited correctly

Exploring the functionality of a password input field, I aim to briefly display the last character entered before reverting back to the concealed password format. Here is a snippet demonstrating this:

var showLength = 1;
var delay = 1000;
var hideAll = setTimeout(function() {}, 0);

$(document).ready(function() {
  $("#password").on("input", function() {
    var offset = $("#password").val().length - $("#hidden").val().length;
    if (offset > 0) $("#hidden").val($("#hidden").val() + $("#password").val().substring($("#hidden").val().length, $("#hidden").val().length + offset));
    else if (offset < 0) $("#hidden").val($("#hidden").val().substring(0, $("#hidden").val().length + offset));

    // Update visible string
    if ($(this).val().length > showLength) $(this).val($(this).val().substring(0, $(this).val().length - showLength).replace(/./g, "•") + $(this).val().substring($(this).val().length - showLength, $(this).val().length));

    // Set timer for hiding characters
    clearTimeout(hideAll);
    hideAll = setTimeout(function() {
      $("#password").val($("#password").val().replace(/./g, "•"));
    }, delay);
  });
});
#hidden {
  opacity: 0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="password" type="text" value="" />
<input id="hidden" type="text" value="" />

The provided code functions as intended, but there is one vexing issue:

If you enter 123456789 and attempt to delete any character in the input field, you will notice that it only removes the last character from the hidden input field (which stores data to be sent to the server).

It seems necessary to identify the position of the text cursor (which can be moved via mouse or keyboard arrow keys) in order to remove the correct character...

What steps should be taken to address this problem?

Note: Despite searching on Stack Overflow, existing codes or jQuery plugins do not adequately resolve this. A comprehensive solution is sought after.

Answer №1

After multiple attempts, I finally managed to achieve what I was aiming for, although there were a couple of cases where things didn't quite come together as planned. However, after putting in the effort, I was able to turn it into an NPM package.

You should give this a try and verify if it works for you as well. It worked perfectly on my end.

npm install last-typed-letter-password

https://www.npmjs.com/package/last-typed-letter-password

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

How can we effectively utilize LESS variables in styles.less when working with LESS files within components in Angular versions 6 or 7?

Running Angular version 7.0.0 will generate a folder structure typical for "ng new". Below is the content of my styles.less file: @personal-black: #0000; This snippet shows the content of my app.component.less file: ...

What is the method to restrict the coverage of the background color within a specific region in HTML?

I am still fairly new to html so I apologize if this is a very basic question for you. Here is the css file that is causing me trouble: .sidepanel-list{ margin-left: 10px; background-color:lightgray; Whenever I run the code, the background color s ...

Issues with FullCalendar.js failing to consistently fire when used in conjunction with JS/Ajax/ColdFusion

Attempting to troubleshoot an issue with FullCalendar.js integration on an external page called "calendar_summary.cfm", which is part of a series of pages reloading on a main page. The data from calendar_summary.cfm is transferred into FullCalendar.js thro ...

Retrieving specific value from a Parent Controller in AngularJS using UI Router

I have a request to display the value stored in $scope.resAVal on my index.html page. This value is accessible within the RootCtrl. index.html <!DOCTYPE html> <html ng-app="plunker"> <head> <!-- any required JavaScript librarie ...

How can I update the title of the NavigationBarRouteMapper without altering the current route in React Native?

Here is a snippet of code that outlines the NavigationBarRouteMapper: var NavigationBarRouteMapper = { ... , RightButton(route, navigator, index, navState){ return( <TouchableHighlight onPress={()=>{ //When this ...

The size of objects on canvas is not consistent when loading with fabric.js loadFromJSON

Click here to view the code var canvas = new fabric.Canvas('canvas_1'); var canvas2 = new fabric.Canvas('canvas_2'); var imgObj = new Image(); imgObj.src = "https://gtaprinting.ca/wp-content/uploads/2021/05/blank-t-shirt-front-gre ...

React Native automatically triggers the onPress event when using withNavigation

It seems that onPress should only be triggered by a touch event. Initially, this was the case, but when using withNavigation in a screen outside of the StackNavigator, onPress appears to render automatically. HomeScreen.js function mapStateToProps(state) ...

Is there a way for me to determine if a user has engaged with a form?

I'm surprised by how difficult it was to find information on this topic through Google. Currently, my struggle lies in wanting my form fields to change color based on validity only after the user has interacted with the form. I don't want invali ...

Ensuring a Memory Leak-Free Environment: Tips and Tricks

Here is my code, and I'm curious if the delete function effectively destroys an object without causing any memory leaks: class Projectile extends MovableObject{ constructor(){ super(); this.speed = 2; this.geometry = new THREE.CircleGeome ...

Mixing pipe operators with Angular Observables in an array

In my project, I have an interesting scenario where I am using Observables and piping them to multiple functions as shown below. getOrders(filters: any, page: any = 1, orderBy: string = 'deliver_on', sort: string = 'asc') { const op ...

Having trouble retrieving a JavaScript Object from a promise in Node.js

I am attempting to retrieve a JSON report with two parts and convert it into a JavaScript object in my mainFile.js, as demonstrated below: file1.js module.exports = async (param1, param2) => { try { await fun1(param1, param2); await ...

Is there a way to iterate through an object while capturing the index of each element and then saving it into an array?

After attempting the following code, I am only receiving a single value as output. Object.keys(yelpResults).map((key) => { return setRestaurantIndexes(restaurantIndexes.concat(key)); }); ...

Setting a Div's Height Relative to Page Height

I have been following a tutorial on adjusting the height of my div container based on page or screen resolution. Even though it seems simple, I am having trouble getting it to work on my own page. When I hover over the container div in the page inspect to ...

The property of the object is not defined

My goal is to pass an array of objects (merchants) into a function, iterate through each 'merchant', and perform an action with the 'merchant_aw_id' of that merchant. However, I am encountering an issue where I am getting undefined. mo ...

What are the steps to resolve the issue of "npm ERR! EEXIST: file already exists, rename" occurring with non-existent files?

Welcome to my first question post. (Please be kind if I make mistakes.) I am using node version 5.6.0. For an assignment, I downloaded a JS web app but am encountering an error that is preventing me from working on it: S:\PersonalCloud\jennyly ...

Reorder data in an array using a specific field in AngularJS

I am working with an array that looks like this: [Object,Object,Object]; Each object in the array has a property named "rate". I need to sort these objects based on their rate property. In my JavaScript, I have a variable called $scope.restaurants.data, ...

The never-ending loop of a jQuery function continues indefinitely

Having some trouble with this code. It's supposed to take the first line, check it, remove it, and then move on to the next line. However, if all lines are empty, the code just keeps running endlessly without stopping. function getSocksListInput() { ...

The limitation of jquery fileupload is that it does not support the selection of files with varying file

Is it possible to prevent files with certain extensions from being uploaded to my server? Specifically, using jQuery fileupload, how can I restrict the selection of multiple file types simultaneously, such as pdf and tif? ...

How to insert a new div element in Wordpress using Advanced Custom Fields

I have a piece of code here that utilizes Wordpress Advanced Custom Fields to generate boxes on a page. I am interested in adding an empty div before every even-numbered div produced, like so: <div class="span2 team-'. $count .'"> </div ...

Is there a way to get ASCII to transmit characters rather than codes?

Is there a way to translate the charCode into the actual character that was typed? For example, if the letter A is entered on a keyboard, how can PHP output the letter A instead of its charCode value? var ucode = function(s) { var len = s.length; ...