What is the best way to create a number input field that also accepts text input?

The goal I have in mind is to create an input field that will contain text like "100%", "54vh", or "32px".

I am attempting to utilize the number input's up and down arrows, while still allowing the user to edit the text. However, it should only allow additions if it recognizes a %, vh, or px symbol in front of the number.

---------------
|  54px  | ^  |
|        | v  |
---------------

Answer №1

If you try adding strings to number inputs, they won't register the value correctly. A good workaround is to use a <select> tag for units and then combine the two values using either JavaScript or PHP. Here's an example:

<input type="number" name="value" value="">
 <select class="" name="unit">
   <option value="">%</option>
   <option value="">px</option>
   <option value="">vh</option>
 </select>

Additionally, it's best practice not to allow special characters like % or / in input fields. It's safer to handle inputs this way.

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 causing the border-radius property to malfunction on a particular website when using Internet Explorer?

We create browser add-ons for popular browsers like Google Chrome, Firefox, Safari, and Internet Explorer. Our add-ons inject HTML elements into various websites such as mail.google.com and email14.secureserver.net. The HTML element we use has a border-ra ...

Which is more memory efficient: creating an object with functions defined on it using a function, or creating an instance of a class?

Imagine if I were to create a hypothetical class (this is purely for demonstration purposes) class X { constructor(word, number) { this.wordNumberString = word + number; } saySomething() { return `${this.wordNumberString} ${this.wordNumberStr ...

Inconsistencies in Height Among JQuery Elements

I am encountering an issue with my datatable.js, where I am attempting to limit its height based on a specific row number, such as 4.5 rows. However, I am facing a problem with the row height (tr height). For example, when using the following method with m ...

an li element is accompanied by a visible box

In my possession is a container: #box1 { height:100px; width:208px; } along with a series <li id="first"><strong>FIRST</strong> </li> <li id="second"><strong>SECOND</strong&g ...

What is a clear indication that a <div> is filled with text?

Picture a scenario where a website contains an element that needs to be filled with random text using JavaScript. Once the div is completely filled, it should reset and begin again. It may sound odd, but the question is: how will the JavaScript determine w ...

Rails vertical scrolling feature for Ruby developers

I'm in need of a vertical slider for my website. I am currently using Ruby 4.0 and have been utilizing the code snippet below to create a horizontal slider: <%= f.range_field :value, :min => 0, :max => 250 %> However, I want the slider b ...

When making jQuery Ajax calls, the $_POST variable may be empty and a warning about headers already being sent may also

I've been working on a PHP project and encountered an issue with sending data from an HTML page using jQuery Ajax to another PHP page. After numerous attempts to debug the problem, I still can't figure out why $_POST is empty when I try to echo. ...

Focused input filter in a tabular header style

I've been working on customizing the Tabulator header filter to have a more "flattened" style, as requested by my boss. In my CSS, I added the code ...input:focus{border:1px solid #1bb394} to change the input border to 1px green. While this change tak ...

Reset the input field once the message has been successfully sent

My goal is to clear the form message input field after the form is sent. However, it seems to be clearing the data before it has a chance to be sent. Here is the code I'm using: <script> $(function () { $('form#SendForm'). ...

Using Javascript to save content to a text file across multiple lines

I am working on a coding project that involves extracting data from a JSON file via a URL and parsing the JSON content. The script successfully filters out the necessary data, but now I want to write this data to a text file with each piece of information ...

Having trouble retrieving JSON data using jQuery and Handlebars?

Hey there, I'm new to JSON and all the talk about JavaScript templating engines for front-end dynamic changes. However, I'm struggling to make progress. Can anyone help me out? The JSON file is named data.json { users: [{ userna ...

How to Track URL Modifications within an Iframe on an Angular Ionic Mobile Application

Issue I'm currently working on identifying modifications in the URL of an embedded <iframe /> within an Ionic (v5) app using Angular. Solution // ts file @ViewChild('myIframe') public iFrame; backButtonSubscription: Subscription; ...

Eliminate the curved edges from the <select> tag

Is there a way to eliminate the rounded corners on a select element? I attempted using the code below, but it resulted in removing the arrows and cutting off the bottom of the placeholder text: select { -webkit-appearance: none; -webkit-border-radius ...

Button placement that feels out of place

I'm looking to style an input-group with a form input and submit button using Bootstrap 5. Here's my current code: .card { margin: auto; width: 50%; padding: 10px; opacity: 0.9!important; top: 250px; } <div class="card-header"> ...

How do I search for a JSON object in JavaScript?

I have an array containing screen resolutions and I need to find the correct resolution range for the user's viewport (I have the width x height of the current window). Here is the sample JavaScript array with JSON objects: [ {width:100,height:200, ...

The Angular application is receiving a 404 error when trying to access the .NET Core

Having trouble calling a method in the controller via URL, as I keep encountering a 404 error. What could be the issue? API Endpoint: http://localhost:5000/Home/HomeTest /*.net core web-api*/ namespace MyApp.Controllers { Route("api/[controller]") ...

Image failed to load

Issue Encountered in Browser Console: https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404 While attempting to display the image on the browser, I am uncertain if the problem lies within my code or with food2fork. Code from index.js: // Alway ...

How to remove checkbox border using HTML, JavaScript, and CSS

Is it possible to remove the square border from a checkbox in HTML? ...

Tips for displaying information in an Autosearch feature

I am trying to implement an auto search feature using AJAX calls, but I am facing difficulties in displaying the results on the search bar. The search results are showing up properly elsewhere. <input type="text" id="select_link" placeholder="enter th ...

Update the package.json file by adding a new command to an existing script

Is it possible to automatically run npm install before starting the application with npm start? Here is what my package.json file currently looks like: . . "scripts": { "test": "echo \"Error: no test specified\ ...