Showcasing text behind an element with reduced opacity when the element directly above it is selected using rails, CSS, and jQuery

I have a password field on my page where the password is hidden, but I want users to be able to copy and paste the clear text version of the password on another website.

In order to achieve this, I followed the instructions in an article titled Mask text, but still allow users to copy it. I created another input[type=text] field with very low opacity (0.001) that overlaps with the password field.

Now, when users try to copy the password from the visible field, they are actually copying the value from the hidden input field with low opacity.

However, I would like to improve the user experience when selecting the password. Currently, users may not realize if their values are being copied because they are copying from an overlapped input field with low opacity.

So, I am looking for a CSS or jQuery trick that will highlight the asterisked password behind the transparent field so that users can see that their values are being copied.

Thank you, Dean

Answer №1

Do you truly believe that you or your users require this feature, or is it simply a convenience? Most password fields do not allow copying, so the number of people utilizing this function may be minimal unless actively promoted. Perhaps incorporating a "copy password" link with ZeroClipboard could provide a more efficient solution?

Here is my suggestion using the jQuery-textrange plugin to facilitate text selection across various browsers. Please note that this method requires relatively recent browser versions. You can view a demo on jsfiddle here.

The approach involves making the low-opacity field unresponsive to mouse events, allowing users to select text in the password field normally without needing to manage selection rendering. This can be accomplished with the following CSS code:

#account-password-hide { pointer-events: none; }

When a user finishes selecting text, we retrieve the selection position and replicate it in the low-opacity field. While this action will clear the selection in the password field, users can now use CTRL+C or right click → copy due to setting pointer-events: auto. If it remains none, right-clicking would direct to the actual password field instead.

Finally, we reset the pointer-events property to ensure subsequent clicks target the correct field:

function reset() {
    $('#account-password-hide').css("pointer-events", "none");
}
$('#account-password-hide').mousedown(function (evt) {
    if (evt.which !== 3) {
        reset();
    }
});
$('#account-password-hide').mouseup(function () {
    reset();
});
$('#account-password-hide').blur(function () {
    reset();
});

In essence, this method appears to be effective. To maintain selection focus during changes, creating an additional element mimicking the characters in the password field and styling them with CSS system colors could be explored. Achieving the appropriate z-index and opacity for each character may necessitate some experimentation.

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

Transfer or duplicate an SVG image from the bottom div to the top div

Is there a way to move a div from the chart_div to the movehere div? I've tried duplicating it below the current svg, but I'm having trouble targeting just the header row ('g' element) specifically. Here is the HTML: <script type= ...

Performing a task after a process has finished

Currently, I'm working with node JavaScript and facing an issue where I need to run a new function after a loop has completed. In the code snippet provided below, // loop through objects in data, to process it represents a basic for loop iterating ove ...

What happens if I don't associate a function or method in the React class component?

Take a look at this straightforward code snippet that updates a count using two buttons with different values. import "./App.css"; import React, { Component } from "react"; class App extends React.Component { // Initializing state ...

A quick guide on automatically checking checkboxes when the user's input in the "wall_amount" field goes over 3

I'm looking to have my program automatically check all checkboxes (specifically "Side 1, Side 2, Side 3 and Side 4") if the input for wall_amount is greater than 3. Is there a way to achieve this? I attempted lines 10-12 in JavaScript without success. ...

Tips for targeting an element for focus following a re-render in ReactJS

Within my web application, when a user hits the enter key, they are able to save the current record. A message confirming that the "record has been successfully saved" is then displayed. However, I have noticed that the blinking cursor in one of the input ...

I am having trouble retrieving values from an Ajax call for my jQuery progress bar

I am attempting to utilize Ajax in order to retrieve a value from a MySQL table and display that information on a progress bar, but unfortunately it is not working as expected. Here is the jQuery code I am using: $(function() { $.get("index.php?page ...

Avoiding the Popover Component from Covering the Screen When the Menu Component Opens in React Material UI

I have implemented a Menu component to display a menu upon hovering over an element. However, I have encountered an issue where the menu includes a Popover component that opens up and covers the entire screen as an overlay, preventing interaction with th ...

In Vue.js, utilize a contenteditable div to dynamically add an HTML element and bind it with v-model

Below is the HTML code I have written. <div id="app"> <button @click="renderHtml">Click to append html</button> <div class="flex"> <div class="message" @input="fakeVmodel" v-html="html" contenteditable="true">< ...

javascript - Retrieving a JSON string

I am currently working on a stock website where I need to retrieve JSON information from either Google's API or Yahoo's API. To test this functionality, I have used a replacement function to log the data onto a text box for testing purposes. Howe ...

Utilizing traditional JavaScript variables within Express.js or Node.js: A comprehensive guide

How can I successfully export variables from regular JavaScript to be used in ExpressJS? I attempted to use 'exports' but it didn't yield the desired results. For instance, in a regular JS file: var search = 'hello'; exports = s ...

Tips for dynamically updating the div class based on the model's value

How can I dynamically change the CSS class of a bootstrap element based on a value? I would like to display a div in green if the value is "OK" and red otherwise. If status = "OK", it should look like this: <div class="small-box bg-green">, else < ...

Guide on creating a cookie verification process with no contents

Request for Assistance: let cartHelper = { cartCookieName: "_cart", getCart: function (callback = undefined) { return apiHelper.getRequest( "/carts", (response) => { documen ...

When running Javascript code locally, the function works perfectly fine. However, once published, the function encounters a

After spending hours working on cascading dropdown lists and finally getting them to function properly, I published the project only to find that it didn't work as expected. The second list failed to populate. Upon checking Firebug's console, an ...

jQuery causing trouble with AJAX in Rails

Currently, I am fetching a list of users from the controller side and generating the HTML code to append it. This is how I wrote the code: $.ajax({ type : "get", contentType : "application/json; charset=utf-8", url : "/users/sear ...

The light/dark mode toggle is a one-time use feature

I've been experimenting with creating a button to toggle between light and dark modes on my website. Initially, it's set to light mode, but when I try switching to dark mode, it works fine. However, the issue arises when attempting to switch back ...

Is it possible to rearrange the order of rows and columns based on the viewport size?

Is it possible to achieve two different layouts with Bootstrap 5 based on the viewport size? I want to avoid using display:none and JS/Jquery tricks if possible. I attempted to accomplish this by utilizing Bootstrap 5 tools, but encountered issues on lar ...

The String returned by out.print() cannot be compared

I am currently utilizing JSP as a server-side script alongside HTML and JQuery for the client end functionality. My AJAX requests to the JSP file are working smoothly with no issues. However, I have encountered a problem when attempting to compare the stri ...

What causes JavaScript to be unable to run functions inside other functions?

Functional languages allow functions to be executed within the argument brackets of nested functions. In JavaScript, which drew inspiration from Scheme, what is the equivalent? f( f ( f ( f))) console.log( 1 + 1 ) //2 How does JavaScript execut ...

Acquiring information to display in a div using innerHTML

I aim to create a div that displays different animal sounds, like: Dog says Bark Bark I've attempted various methods but I'm unable to get each pair in the array to show up in a div: const animal = [ {creature: "Dog", sound: "B ...

"Master the art of using express and jade to loop through data and generate dynamic

Hey there! I have a question regarding node.js. When working with express, we typically have multiple files such as app.js, index.jade, index.js, and server.js where most of the server logic resides. Let's say we have two objects defined in server.js ...