Prevent a user from being able to highlight text in a text input field

I'm a little baffled as to why this isn't working as expected.

The input I have is being masked successfully, but when the text is selected, it shows the actual value.

To address this issue, I want to prevent users from highlighting the input text. While some may argue that this compromises usability, I am specifically focusing on touchscreens where excessive tapping could lead to unwanted selections.

Although I've tried adding

-webkit-user-select: none;  
 -moz-user-select: none;    
 -ms-user-select: none;      
 user-select: none;

to both inputs, the selection still persists. Are there any alternative methods to achieve this? This is all the information I could gather during my search.

Feel free to experiment with a fiddle of the issue here.

Answer №1

If you're looking to achieve this method, consider utilizing pointer-events: none for the lower input and text-indent: -9999px to eliminate the visible text.

Downside

  • No cursor in the input (although this could potentially be emulated with javascript)

I recommend using:

<input class='value' type="password" />
to prevent selecting the text with ctrl + a and copying it.

Try it out on JSFiddle!

CSS

.num {
    position: relative;
}
.value {
    text-indent: -9999px;
    position: absolute;
    top: 0;
    left: 0;
    background: transparent;
}
.number {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

Answer №2

To avoid completely disabling text selection, one option is to identify when text has been selected, then either clear the selection or shift focus to a different element.

For more information, please refer to: Detect what is selected (highlighted) or clicked within an element on a page?

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

Trigger Heartbeat Signal on Page Access using jQuery

I have created the following jQuery script to send a Heartbeat message back to PHP every 3 seconds, and it is functioning correctly. The purpose of this script is to track which users are currently logged in and active on the site. setInterval(function () ...

Darken the entire webpage and gradually fade in a specific div element using Jquery

Currently, I am working on implementing the following functionality: - When a link is clicked, it should trigger a function to show a DIV (#page-cover) that will dim down the entire background. This div has a z-index of 999. - Following the dimming effect, ...

Exploring the process of dynamically inserting choices into an array of HTML dropdown menus using a combination of Php, AJAX, and jQuery

My task involves populating an HTML array of 10 select fields with jQuery every time a div-popup is called. The purpose of this form is to facilitate the batch approval of staffing requests for hundreds of employees by upper management, organized by depart ...

Enhancing JavaScript Variables Through Dynamic Ajax/PHP Interaction

I've been encountering some difficulties when trying to refresh my game. Initially, I attempted echoing the entire script each time... but that method was not aesthetically pleasing. Currently, I am experimenting with updating the variables through an ...

"Troubleshooting: Why is my Bootstrap modal window only printing a

I've encountered an issue with printing the content of a Bootstrap modal window. Previously, the code I had was working fine but now it seems to be malfunctioning. Content is dynamically added to the form using appendChild() in a separate function. Ho ...

Ways to retrieve and contrast the border style of an image utilizing javascript

I'm currently attempting to identify images on my webpage that have the style border: 1px #c6c6c6 solid; let images = document.getElementsByTagName('img'); for (let i = 0; i < images.length; i++) { if (images[i].style.border === "1px ...

Utilizing Javascript in Owl Carousel 2 to Filter Items While Preserving the Sort Order

Currently, I am utilizing Owl Carousel 2 in combination with a filtering example to create a filter menu. When titles in the filter menu list are clicked, all other items in the carousel disappear. This functionality works flawlessly, except for the fact t ...

Unable to fetch css file in Node.js

I am currently in the process of learning Node.js, but I have encountered a small issue with retrieving data from a css file. Interestingly, when I directly add the data to the index file's code, it seems to work perfectly. Let me share the relevant ...

The debugging tool couldn't pinpoint the error, which is quite odd

I'm going crazy... there's an error in the script but I can't seem to find it, and oddly enough, even the Explorer debug doesn't catch it!! I've checked it multiple times with the exercise but I just can't locate it. <s ...

The jQuery UI Tab is failing to scroll within its container

Scenario : I am facing an issue with a scrollable container in IE8. The containing div is supposed to be scrollable and it holds my jquery UI tab div. Issue: While scrolling the container in IE8, other content within it also scrolls, but the jQuery UI t ...

How can I incorporate a custom color into a preset navbar on an HTML webpage?

<div class="navbar-header"> <button aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" style="color: blue;" class="navbar-toggle collapsed" type="button"> <i class="fa fa-reo ...

How can I apply Ben Alman's debounce jQuery function in my code?

I am currently utilizing a project found at https://github.com/cowboy/jquery-throttle-debounce My code seems to be somewhat functional, however the debounce feature is not functioning as expected. This results in the waterfall function being triggered for ...

Creating a Queue Table with PHP, MySQL, and Ajax in Action

My database has two tables - schedule with columns id, date, time, cabinet, and another table cabinets with columns id, cabinetNumber, title. I am trying to generate a table resembling a queue table with the following structure: cabinet 8,9,10,11,12,13,14 ...

The YYYY-dd-mm input mask pattern is malfunctioning

Hello, I am having trouble creating a pattern for an input field in the format YYYY-mm-dd. My code is not working properly. Can someone please help me correct my code? Any assistance would be greatly appreciated. <html> <head> <title>En ...

Obtaining a CSS element again to be utilized for boundary checking purposes

Is it possible to retrieve a CSS element? I recently found myself in a situation where I needed to add some code to an Adobe Edge project, specifically involving the manipulation of margin-top and margin-left properties. While experimenting with moving an ...

Implementing data updates in Ruby on Rails through AJAX or jQuery

Within a text field input lies the value of a database attribute (ref). Upon focusing on the field, a border appears and disappears upon clicking out. My dilemma is that I wish for the data within the text field to be saved in the database without the nee ...

The jQuery Click function is being triggered repeatedly

Below is a code snippet to examine: $(function(){ $(".notes").click(function(){ console.log('hi'); }) }) <a data-shipmentid="1" href="#" class="notes btn btn-primary btn-xs">Notes</a> <a data-sh ...

Using jQuery to select the nth element of a list

I have a challenge where I need to insert an hr tag after the fourth element in a container. When I do this, it interferes with my jQuery code that is supposed to remove the padding-right from the fourth element. The jQuery works fine without the tag. $ ...

Adjust the image's height to adapt to the size of the browser

I'm having trouble resizing the images to match the height of the browser window. My goal is to limit the images' height to a maximum of 1100px and have them scale down based on the height of the browser. I've experimented with setting the ...

Supply a variety of values to jQuery

Recently, I created a loop to retrieve multiple rows from the database. Each row includes a link and a hidden input with a value of posting_id. Essentially, this functions similarly to a "like" button on Facebook. The hidden input stores the posting_id, wh ...