Develop a slider feature based on radio button selection

I am currently working with radio buttons for ticket selection.

 <div>
 <input type="radio" name="ticket" value="Standard"> Standard
 <input type="radio" name="ticket" value="Express"> Express
 <input type="radio" name="ticket" value="Priority"> Priority
 <input type="radio" name="ticket" value="Overnight"> Overnight
</div>

I am interested in converting the radio buttons into a slider using JavaScript, similar to this:

https://i.sstatic.net/HhRBJ.png

After researching online, I found a slider example on

w3schools

However, it does not offer a way to set specific selection points (breakpoints).

Additionally, I came across

<input type="range" id="myRange" value="90">

but I'm unsure how to create a range with limited values instead of a complete slider.

Answer №1

Enhance your slider by segmenting it into sections with a touch of JavaScript and CSS magic. I've included markings to mimic the visual appeal you desire from the image in your query.

var slider = document.getElementById("myRange");
var output = document.getElementById("shipping");
output.innerHTML = slider.value; // Display the default slider value

var labels = ['Standard', 'Express', 'Priority', 'Overnight'];

slider.value = 0;
sliderInputChange();

// Update the current slider value (each time you drag the slider handle)
slider.oninput = sliderInputChange;

function sliderInputChange() {
  var val = slider.value;
  var unit = 12.5;
  var optionNum;

  if (val <= 25) {
    slider.value = unit;
    optionNum = 1;
  } else if (val <= 50) {
    slider.value = 25 + unit;
    optionNum = 2;
  } else if (val <= 75) {
    slider.value = 50 + unit;
    optionNum = 3;
  } else {
    slider.value = 75 + unit;
    optionNum = 4;
  }
  output.innerHTML = 'Shipping: ' + labels[optionNum - 1];
}
@import url('https://fonts.google.com/?query=lato&selection.family=Lato:300');

body {
  margin-left: 0;
  margin-right: 0;
  text-align: center;
}

.slider {
  -webkit-appearance: none;
  appearance: none;
  height: 2px;
  background: black;
  outline: none;
  width: 100%;
  margin-left: 0;
  margin-right: 0;
}

.mark {
  width: 2px;
  height: 30px;
  background-color: #aaa;
  position: fixed;
  top: 6px;
  z-index: -1;
}

.mark1 {
  left: 13%;
}

.mark2 {
  left: 38%;
}

.mark3 {
  right: 13%;
}

.mark4 {
  right: 38%;
}


/* Customize slider handle appearance */

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 15px;
  height: 15px;
  background: black;
  cursor: pointer;
  border-radius: 50%;
  margin: 0;
}

.slider::-webkit-slider-thumb,
.slider::-moz-range-thumb {
  width: 15px;
  height: 15px;
  background: black;
  cursor: pointer;
  border-radius: 50%;
  margin: 0;
}

#shipping {
font-family: "Lato";
font-size: 2rem;
font-weight: 300;
}
<div class="container">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <div class="mark mark1"></div>
  <div class="mark mark2"></div>
  <div class="mark mark3"></div>
  <div class="mark mark4"></div>

  <p id="shipping"></p>
</div>

Answer №2

Adjust the example code snippet below to customize the range slider options:

var steps = [
    "Low",
    "Medium",
    "High",
    "Urgent"
];
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = steps[slider.value]; // Display the default slider value

// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
    output.innerHTML = steps[this.value];
}
#slidecontainer {
    width: 100%;
}

.slider {
    -webkit-appearance: none;
    width: 100%;
    height: 15px;
    border-radius: 5px;
    background: #d3d3d3;
    outline: none;
    opacity: 0.7;
    -webkit-transition: .2s;
    transition: opacity .2s;
}

.slider:hover {
    opacity: 1;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
}

.slider::-moz-range-thumb {
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
}
<div id="slidecontainer">
  <input type="range" min="0" max="3" value="3" class="slider" id="myRange">
  <p> <span id="demo"></span></p>
</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

AngularJS Module Instantiation Error

My angularjs module is not loading correctly and I'm struggling to figure out why. Despite reading numerous tutorials, I can't seem to get it to work. [17:22:36.338] Error: [$injector:modulerr] Failed to instantiate module wc2013mongoMod due t ...

The res.render function is failing to render the view and instead, it is generating

When making a call to express/node like this: jQuery.ajax({ url : url, type: "GET", data: {names : names}, success: function(data) { console.log("ajax post success"); }, ...

Text input fields within a grid do not adjust to different screen sizes when placed within a tab

I noticed that my component under a tab is causing the Textfield to become unresponsive on small screens. To demonstrate this, I checked how the Textfield appears on an iPhone 5/SE screen size. https://i.stack.imgur.com/d8Bql.png Is there a way to make t ...

Enhancing the appearance of an Angular.js application

On the same page, there are two buttons - one for buying and one for selling. It appears that both buttons share the same HTML instance. This creates a problem when trying to style them individually using classes, ids, or other HTML-targeted selectors, as ...

Switch Cursor on Movable Area in Electron

Working on a project in Electron and having some trouble with a frameless window. I have designated certain top areas as draggable using -webkit-app-region: drag, but the cursor will not change as expected. Although this code snippet won't make the e ...

Translating from JavaScript to Objective-C using JSON

Can someone help me figure out how to correctly 'return' this JSON object in JavaScript? function getJSONData() { var points = '{\"points\": ['; var params = polyline.getLatLngs(); ...

Best practices for utilizing the getTile() method within Google Maps

I have a question about storing markers in a database using tile IDs. The goal is to display all the markers associated with a specific tile when it is displayed on a map. Initially, I created a code that was not working correctly. It only requested one ...

What is the best way to add a dropdown menu in front of a button?

I have a div containing a dropdown list of items and I want the user to be able to add more dropdown lists by clicking a button. Although I managed to do this, the issue I'm encountering is that the new list gets added after the button instead of ben ...

Netbeans fails to detect Jquery

Looking for some assistance here. I'm in the process of setting up my computer for a new project and for some reason, Netbeans isn't recognizing any of the .js files. Any tips on how to enable it to start recognizing these files? ...

Learn how to display each element in a list one by one with a three-second interval and animated transitions in React

Let's consider a scenario where we have a component called List: import React, { Component } from 'react'; class List extends Component { constructor() { super(); this.state = { list: [1, 2, 3, 5] } ...

When I use the select picker class, the <select> tag does not appear as intended

Is there a way to retrieve data from the second select, specifically the one with the class 'select picker'? I am encountering an issue where the data is not displaying when using the bootstrap-select component. I have confirmed that the componen ...

What sets apart passing arguments to a function from utilizing variables at the class level?

As someone who is just starting out in the Typescript and Angular-2 world, my previous experience includes working with Java and Angular-1.5. Imagine a scenario where there is a component class with several variables that need to be used across functions, ...

How can you calculate the number of elements in a jQuery array and iterate through each of them?

After extracting a string from a mySQL query with PHP, my AJAX script comes into play. This string is then dissected and placed into a jQuery array. The results are displayed on the screen using .html() The length of this array can range from zero items t ...

Troubleshooting a Node.js problem with variable scope

I'm working on a nodejs route that downloads a URL as an mp3 using npm-youtube-dl. I have a download directory being monitored with chokidar for new files, and once a file is added, I save the file link. After the download completes, a function is cal ...

Error encountered when dividing MySQL data into three separate columns

Greetings, We are currently working on a script to split the data retrieved from a mysql database into a table with three columns. However, we are facing an issue as the database returns 8 values but 9 columns are appearing (the last one is completely emp ...

Exploring IndexedDB with Multi-dimensional Relationships

How do you manage many-to-many relationships in IndexedDB systems? For instance, let's consider a scenario where there is a Blog object to represent a blog post and a Tag object for tagging the posts. A single Blog can have multiple tags, and each ta ...

Why is Angular.orderBy not displaying any data on the Page?

Embarking on my first Angular project with Firebase Firestore, I am excited to showcase some of the code below. I recently learned Angular and here is what I have accomplished so far: Component.html <section class="rank"> <p class=& ...

The font size on my website fluctuates up and down every time I refresh the page or navigate to a different one

I am currently in the process of updating my research lab's website using a Jekyll framework with Bootstrap. However, I have encountered an issue where the font size grows slightly whenever I navigate to a new page or refresh the current one, before r ...

Deno -> What steps should I take to ensure my codes run smoothly without any errors?

Trying to import locally Experimenting with Deno v1.6.0 in a testing environment Attempting local imports using Deno language Local directory structure: . └── src └── sample ├── hello_world.ts ├── httpRequest. ...

Utilize JavaScript to Extract Information from an Array of JSON Objects

Struggling to efficiently handle an array of JSON objects using JavaScript. The data fetched from my PHP file looks like this: Array ( [0] => Array ( [id] => 1 [name] => holly [text] => Text 1 ...