Displaying various results using a range slider

I really need some assistance in making this range slider produce multiple outputs. I've attempted a few different options, but unfortunately, I haven't been able to figure it out.

My goal is to have the text "590" display as 5.9 times the value of the slider with commas for numbers in thousands and the text "30,000" to show as 300 times the slider value also with commas.

[JSFiddle Demo]

var element = document.querySelector('input[type="range"]');

var updateRangeValue = function(){
  var newValue = element.value;
  var targetElement = document.querySelector('.value');
  target.innerHTML = newValue;
}

element.addEventListener("input", updateRangeValue);
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>

Answer №1

I made some updates to the Javascript code.

Whenever the range value is adjusted, a calculation is performed as follows: 5.9 * newValue. To format the result with commas, I referred to this particular solution, utilizing parseInt() to ensure proper rounding and avoid odd numbers like 1003.0000000000001.

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  document.querySelector('.value').innerHTML = newValue;
  var perYear = insertCommas(5.9 * newValue);
  document.querySelector("#perYear").innerHTML = perYear;
  var perTwentyFive = insertCommas(300 * newValue);
  document.querySelector("#perTwentyFive").innerHTML = perTwentyFive;
}

elem.addEventListener("input", rangeValue);

function insertCommas(x) {
  return parseInt(x).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>

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

Removing the 'div' tag using jQuery in CodeIgniter

I need assistance in removing the added item. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form ...

Implement image effects in a single div by leveraging the jquery functions .fadeIn/Out and .delay(300)

Is it possible to use jQuery to achieve the following task? I currently have a <div> with an id="divid", and inside is an <image source="images/myimage01.jpg">. I am looking for a script that can sequentially load/loop images from <img src ...

The functionality of the web application is not supported in Multi-Device-Hybrid-Apps format

I am encountering an issue with my app that functions perfectly as a typical web app, but fails to work properly when converted into a Multi-Device-Hybrid-App format. The problematic sections are indicated in a screenshot of the app. Below is my complete c ...

Utilizing asynchronous methods within setup() in @vue-composition

<script lang="ts"> import { createComponent } from "@vue/composition-api"; import { SplashPage } from "../../lib/vue-viewmodels"; export default createComponent({ async setup(props, context) { await SplashPage.init(2000, context.root.$router, ...

Creating a node.js function that can be used with OracleDB

I'm currently delving into learning nodeJS, but I'm facing a roadblock and can't seem to figure out what's causing the issue. Even the Debugger isn't providing much help. Any assistance or guidance would be greatly appreciated. The ...

When using jQuery to parse JSON, I am unable to make changes to an array

inventory = new Array(); $.getJSON('items.php', function(data){ $.each(data , function(i,jsonData) { inventory[1] = "item"; }); }); alert(inventory[1]); This code is supposed to display the items in the inventory, but it's ...

Syntax Error: The function `loadReposFromCache(...).error` is not defined in this building

I'm currently attempting to utilize the SyntaxHighlighter v4 plugin, but I'm facing issues with the build process! While following the guidelines provided here, an error popped up: $ ./node_modules/gulp/bin/gulp.js setup-project [10:12:20] Requ ...

Using Vuex as a global event bus ensures that all subscribers will always receive notifications for

For a while now, I have relied on a global event bus in Vue - creating it as const bus = new Vue(). It works well, but managing subscriptions can get tedious at times. Imagine subscribing to an event in a component: mounted() { bus.$on('some.event ...

Comparing the efficiency of using arrays versus mapping to an object and accessing data in JavaScript

When considering the basics of computer science, it is understood that searching an unsorted list typically occurs in O(n) time, while direct access to an element in an array happens in O(1) time for HashMaps. So, which approach yields better performance: ...

ERROR TRACKER: Unable to locate file "CL.exe". File not found in system

I am attempting to run the following command in a Node.js project on my Windows 8 machine: npm install [email protected] However, I am encountering an error that I am not sure how to resolve. TRACKER : error TRK0005: Failed to locate: "CL.exe". ...

Encountered a Vue.js Vuex error: "Unable to dispatch function in context."

I have implemented a stopwatch function that can run the stopwatch as shown below : Stopwatch.vue <script> export default { data() { return { time: "00:00.000", timeBegan: null, timeStopped: null, stoppedDurat ...

Determining the background image size of a div when the window is resized

I'm facing a problem that I can't seem to solve. I have a div with a background image. <div class="a"></div> I want to make a specific point of this background image clickable. I know I can achieve this by adding a div with a z-inde ...

Sort div elements based on checkbox filters

I have a set of checkboxes that I want to use for filtering purposes. <div class="filter"> <div class="checkbox"> <label><input type="checkbox" rel="canada"/>Canada</label> </div> <div class="chec ...

Creating messages from an array using vanilla JavaScript or jQuery

I have an array of objects containing messages that I want to display on my webpage, but I'm unsure how to approach it. [{"message":"dwd","user":"csac","date":"07.04.2021 20:46"}, {"mes ...

What causes the "undefined" error in Node.js when using a

Currently, I am utilizing the node-craigslist package for scraping listings from craigslist. However, I have encountered an issue when processing the results. client .search(options, '') .then((listings) => { listings.forEach((listing ...

Issue: Assertion violation: The use of <Link> element is restricted to within a <Router>. Any solutions or suggestions?

In my React and Next Js application, I am utilizing react-router-dom for routing purposes. The dependencies listed in the package.json file are as follows: This is how my current package.json file looks: { "name": "MUSIC", "versio ...

Tips for removing all elements from an array using jQuery

let values=[]; values.push(data); I have decided to use an array to keep track of my data, and then send it via ajax. The first submission goes smoothly. However, on subsequent submissions, null values are also sent along with the actual data to the serv ...

Top method for organizing an array based on an object's property and displaying the outcome

I encountered a problem for which I couldn't find an immediate solution. I have an array of objects representing products, with each product having a category property. My goal is to group these products by their categories. After some trial and error ...

Display HTML code inside a specified div

I am attempting to incorporate content from another HTML file into my current web page using jQuery's .load method. Unfortunately, the content is not loading as expected. Can anyone provide me with a solution to this issue? Below is the HTML and jQ ...

How can I find the week of the month using Moment.js? (similar to Google Calendar)

Currently leveraging the fantastic features of Moment.js, I am facing a dilemma. How can I determine which week of the month a particular date falls into? The documentation for Moment js only seems to provide information on "week of year." For instance, if ...