Select menu with personalized choices

I am in need of a specific feature for my website. Currently, I have implemented a basic select element but I want to add the option for users to input a range as the last item. Here is an example of what I'm trying to achieve:

<select>
  <option>0-9</option>
  <option>10-19</option>
  <option>20-29</option>
  <option>
    <input type="number" name="min" min="0" max="30"/>
    <input type="number" name="max" min="0" max="30"/>
  </option>
</select>

Unfortunately, this setup does not work as intended. Does anyone have any suggestions on how to properly implement this feature? Perhaps there is a different approach that would work better?

Answer №1

Feel free to give it a go at this link

<div id="container">
  <select id="select-range" onchange="onChangeSelect(this)">
    <option>0-9</option>
    <option>10-19</option>
    <option>20-29</option>
    <option>custom</option>
  </select>

  <div>
    <input id="input-from" type="number" name="min" min="0" max="30" disabled />
    -
    <input id="input-to" type="number" name="max" min="0" max="30" disabled />
  </div>

  <button onclick="onSelect()">choose</button>
</div>

<script>
  const onChangeSelect = (event) => {
    if (event.value === "custom") {
      document.getElementById("input-to").disabled = false;
      document.getElementById("input-from").disabled = false;
    } else {
      document.getElementById("input-to").disabled = true;
      document.getElementById("input-from").disabled = true;
    }
  };

  const onSelect = () => {
    const selectRange = document.getElementById("select-range").value;
    let range = [];

    if (selectRange === "custom") {
      const inputTo = document.getElementById("input-to").value;
      const inputFrom = document.getElementById("input-from").value;
      range[0] = inputFrom;
      range[1] = inputTo;
    } else {
      range = selectRange.split("-");
    }

    alert(`range: ${range[0]} - ${range[1]}`);
    //perform action with range, such as sending it to the server
  };
</script>

<style>
  #container {
    display: flex;
    flex-direction: column;
    align-items: start;
    gap: 10px;
  }
</style>

You'll end up with something along these lines

Answer №2

If you're looking to enhance the selection process by including input fields, it's recommended to opt for a more efficient approach. Instead of customizing the entire selection, consider adding an option labeled 'Custom'. Upon selecting this option, display the input field for a streamlined experience.

function showInput(){
const rangeSelector = document.getElementById("range-selector")
const customRangeInput = document.getElementById("custom-range-input")
if (rangeSelector.value=="Custom Range"){
customRangeInput.style.display = "block"
}
else{
customRangeInput.style.display = "none"
}

}
<select id="range-selector" onclick="showInput()">
  <option>0-9</option>
  <option>10-19</option>
  <option>20-29</option>
  <option >
    Custom Range
  </option>
</select>
<div id="custom-range-input" hidden>
    <input type="number" name="min" min="0" max="30"/>
    <span>-</span>
    <input type="number" name="max" min="0" max="30"/>
</div>
</div>

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

Explore the category options in the reference document using Node.js

Category Schema const CategorySchema = mongoose.Schema( { catName: { type: String, required: true } }, { timestamps: true } ); Product Schema const ProductSchema = new mongoose.Schema( { brand: { type: String, required: true }, title: ...

abandoning the upload of an item into a THREE.js environment

Currently, I am working on a THREE.js scene where I need to prevent uploading multiple files into the scene simultaneously. The setup involves using Angular to implement the three js scene and canvas as a factory to maintain only one instance of a canvas a ...

Most efficient method for revealing all concealed divs using JavaScript

I have a large div containing multiple hidden divs that can be displayed individually or all at once. Here is an example of how it's structured: <div class="maindiv"> Print "<a href=javascript:show()>Click here to show all</a> ...

A step-by-step guide on storing JSON data into a variable using NodeJS

Just starting out with Node.js and running into an issue. I need to figure out how to extract and save data from a JSON object retrieved from the GitHub API. var http = require("http"); var express = require('express'); var app = express(); var ...

Retrieve unique elements from an array obtained from a web API using angular brackets

I've developed a web application using .NET Core 3.1 that interacts with a JSON API, returning data in the format shown below: [ { "partner": "Santander", "tradeDate": "2020-05-23T10:03:12", "isin": "DOL110", "type ...

Managing various iterations of identical repositories

Currently in the process of constructing a library of unique react components that are being utilized in various downstream applications. To incorporate these components into my downstream applications, I rely on the package.json. Recently, I began ponde ...

Mobile Image Sizing with Bootstrap 4 Cards

I am currently developing a responsive web application using Bootstrap 4 and incorporating cards with images on one of the pages. The layout functions properly when resizing the window on desktop view; however, upon viewing it on mobile devices, I noticed ...

Is utilizing React function components the most effective solution for this problem?

export default function loginUserUsing(loginType: string) { const [state, setState] = useState(loginType); function login() { // body of the login function } function oauth() { // body of the oauth function login(); ...

Encountering a Vercel deployment failure due to a TypeError: The 'split' property cannot be read from undefined within several objects

I'm having trouble deploying my web application for the first time and encountering this error on Vercel: TypeError: Cannot read property 'split' of undefined at Object.3qS3 (/vercel/path0/.next/serverless/pages/[collection]/[templateId].j ...

Encountering a Node V18 Peer Dependency Conflict错

Can someone please help me understand what's causing this error? Every time I try to install a dependency, this keeps popping up. I'm completely lost and unsure of what's happening. npm ERR! 1 more (the root project) npm ERR! peer ...

Utilizing JavaScript to eliminate objects from a collection and showcase a refreshed arrangement

On a simple webpage using bootstrap and js/jquery, data is fetched via ajax to create entries displayed to the viewer. The process involves: - making a request - parsing XML to create an array of objects with all the data - sorting by one property - cr ...

Gorgeous stew, precise match when using the "findAll()" function

Currently, I am utilizing Python (3.5), Selenium (3.6), and Beautiful Soup (4.6) to scrape a website. The code snippet I have implemented to locate a specific HTML tag is as follows: descContainer=descContainers[0].findAll("div", {"class":"userHtml"}) Re ...

Next.js application deployment halted due to an unsuccessful completion of the process with the command "/bin/bash -ol pipefail -c npm ci"

After completing the development of a Next.js application (discord clone), I encountered an issue while trying to deploy it on Railway. The error message was: Dockerfile:20 ------------------- 18 | ENV NIXPACKS_PATH /app/node_modules/.bin:$NIXPACKS_P ...

What accounts for the different behavior of line-height when using units vs percentage or em in this instance?

I'm puzzled by the behavior of the CSS code provided, which is also demonstrated in this fiddle. <style type="text/css"> p { font-size: 14px; } .percentage { line-height: 150%; } .em-and-a-half { line-height: 1.5em; } .decimal { ...

What initiates the call for CSS?

During his instructional video, the creator visits the CodeIgniter website at . When he initially arrives at the site (at 1:32), it appears somewhat odd, almost as if it lacks CSS styling. However, after refreshing the page (1:37), it displays properly. ...

Styling a half circle in React Native

Despite my efforts, I have not been able to find anything quite like this: example Could anyone provide guidance on implementing this with react? ...

IE causing Ul LI Menu to not display as Block

I'm encountering an issue with my menu. It displays correctly in Firefox and Chrome, but in IE8 it appears as a vertical list instead of a horizontal menu. I have included a doctype and I am linking to the HTML5 JavaScript via Google, so I'm not ...

Applying a CSS class (or style) dynamically depending on the variable with the help of a directive

I'm facing a situation where I need to apply ng-style (or ng-class) multiple times depending on a variable. However, this repetitive task of writing ng-class for the same functionality for each widget is quite cumbersome for me. Is there a way to si ...

Developing an npm console application that is "installable" similar to tools like yeoman, gulp, or grunt

Recently dipping my toes into the world of NPM, I've been itching to create a package that functions as a console app (think gulp and grunt). My goal is simple: I want users to be able to run npm install -g mypackage followed by mypackage This sh ...

Developing play and pause capabilities using JavaScript for the existing audio player

Currently, I am developing a Chrome extension that includes an HTML popup with buttons to play audio files. However, I feel that my current approach is not the most efficient and I am struggling to find ways to simplify the process. The method I am using n ...