Adjust the pricing using a range input

Currently in the process of setting up a pricing table that adjusts based on the number of users being registered. You can test the functionality on our development site here. My main concern is how to dynamically change the price displayed according to the user input range, as outlined in our pricing structure table located at this link.

To achieve this, I am incorporating an input range from CodePen for user convenience: CodePen Input Range

For displaying the price, we have implemented the following code:

<div class="price-card--price-text">
    <div class="price-card--price-number toggle-price-content odometer" data-price-monthly="18.71" data-price-yearly="224.52">18.71</div>
</div>

The input range setup looks like this:

<p> Number of users </p>
<div class="range">
    <input type="range" id="slider2" min="1" max="5" steps="1" value="1">
</div>
<ul class="range-labels">
    <li class="active selected">1</li>
    <li>2-20</li>
    <li>21-99</li>
    <li>100-999</li>
    <li>1000+</li>
</ul>

Additionally, here is the JavaScript function responsible for toggling the price content between monthly and yearly:

function togglePriceContent() {
    if ($(toggleSwitch).is(":checked") === true) {
        // switch to yearly pricing
        $(".toggle-price-content").each(function() {
            $(this).html($(this).data("price-yearly"));
        });
    } else {
        // switch to monthly pricing
        $(".toggle-price-content").each(function() {
            $(this).html($(this).data("price-monthly"));
        });
    }
}

Answer №1

This solution is effective. To verify, test it within the range of 2-20. I am assuming a 10% discount.

var sheet = document.createElement('style'),
  $rangeInput = $('.range input'),
  prefs = ['webkit-slider-runnable-track', 'moz-range-track', 'ms-track'];

document.body.appendChild(sheet);

var getTrackStyle = function(el) {
  var curVal = el.value,
    val = (curVal - 1) * 24.666666667,
    style = '';

  // Set active label
  $('.range-labels li').removeClass('active selected');

  var curLabel = $('.range-labels').find('li:nth-child(' + curVal + ')');

  curLabel.addClass('active selected');
  curLabel.prevAll().addClass('selected');

  // Change background gradient
  for (var i = 0; i < prefs.length; i++) {
    style += '.range {background: linear-gradient(to right, #37adbf 0%, #37adbf ' + val + '%, #fff ' + val + '%, #fff 100%)}';
    style += '.range input::-' + prefs[i] + '{background: linear-gradient(to right, #37adbf 0%, #37adbf ' + val + '%, #b2b2b2 ' + val + '%, #b2b2b2 100%)}';
  }

  return style;
}

$rangeInput.on('input', function() {
  sheet.textContent = getTrackStyle(this);
});

// Update input value on label click
$('.range-labels li').on('click', function() {
  var index = $(this).index();

  $rangeInput.val(index + 1).trigger('input');

});

$(document).ready(function() {
  $("#range").change(function() {
    console.log($(".range-labels li.active").text());
    var user = $(".range-labels li.active").text();
    if (user == "2-20") {
      var second = $('#standard').data('price');
      console.log(second);
      var discount = second - (second * 0.10);
      console.log(discount);
      $('#standard').text(discount.toFixed(2));
    }
  });
});
.range {
     position: relative;
     width: 300px;
     height: 5px;
}
 .range input {
     width: 100%;
     position: absolute;
     top: 2px;
     height: 0;
     -webkit-appearance: none;
}
 .range input::-webkit-slider-thumb {
     -webkit-appearance: none;
     width: 14px;
     height: 14px;
     margin: -3px 0 0;
     border-radius: 50%;
     background: #37adbf;
     cursor: pointer;
     border: 0 !important;
}
 .range input::-moz-range-thumb {
     width: 14px;
     height: 14px;
     margin: -3px 0 0;
     border-radius: 50%;
     background: #37adbf;
     cursor: pointer;
     border: 0 !important;
}
 .range input::-ms-thumb {
     width: 14px;
     height: 14px;
     margin: -3px 0 0;
     border-radius: 50%;
     background: #37adbf;
     cursor: pointer;
     border: 0 !important;
}
 .range input::-webkit-slider-runnable-track {
     width: 100%;
     height: 8px;
     cursor: pointer;
     background: #b2b2b2;
     border-radius: 3px;
}
 .range input::-moz-range-track {
     width: 100%;
     height: 8px;
     cursor: pointer;
     background: #b2b2b2;
     border-radius: 3px;
}
 .range input::-ms-track {
     width: 100%;
     height: 8px;
     cursor: pointer;
     background: #b2b2b2;
     border-radius: 3px;
}
 .range input:focus {
     background: none;
     outline: none;
}
 .range input::-ms-track {
     width: 100%;
     cursor: pointer;
     background: transparent;
     border-color: transparent;
     color: transparent;
}
 .range-labels {
     margin: 18px -41px 0;
     padding: 0;
     list-style: none;
}
 .range-labels li {
     position: relative;
     float: left;
     width: 75.25px;
     text-align: center;
     color: #b2b2b2;
     font-size: 14px;
     cursor: pointer;

}
 .range-labels .active {
     color: #37adbf;
}
 .range-labels .selected::before {
     background: #37adbf;
}
 .range-labels .active.selected::before {
     display: none;
}
 body, html {
     height: 100%;
}
 body {
     background: #f2f2f2;
     display: flex;
     align-items: center;
     justify-content: center;
}
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <div class="range">
    <input id="range" type="range" min="1" max="5" steps="1" value="1">
  </div>

  <ul class="range-labels">
    <li class="active selected">
    1</li>
    <li>2-20</li>
    <li>21-99</li>
    <li>100-999</li>
    <li>1000+</li>
  </ul>
</div>

<div data-price="18.71"id="standard">18.71</div>
<div data-price="30.71" id="premium">30.71</div>
<div data-price="41.21" id="enterprise">41.21</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

Communicating parameterized information to ASP.NET server-side with jQuery by utilizing $.post method, rather than resorting to $.ajax

I am utilizing jQuery to store data in my database within an MVC 5 application using ASP.NET. The information sent includes an id parameter of type int and an array of data objects. While sending the data to the server-side works with $.post, it results i ...

Updating the JSON data with a new row

I am trying to dynamically add a new row to my JSON data when the user clicks on a link. Despite not receiving any errors, I am unable to see the updated JSON in my alert message. $(document).ready( function(){ people = { "COLUMNS":["NAME","AGE"], ...

JQuery continuously refreshing the page causes browser memory overload

I've run into an issue with my jQuery code that fetches a generated HTML page every second, eventually causing an out-of-memory error. How can I address this problem? $(document).ready(function() { setInterval(function() { $.g ...

Strategies for creating a grid-inspired layout: tips and tricks

Recently, I've been tasked with creating a catalogue section for our e-commerce site. My designer came up with this layout which I find really appealing, but I'm having trouble figuring out how to implement it. I attempted using tables, but it di ...

The functionality of jQuery's appendTo method seems to be malfunctioning

I am trying to display an image with a popup using jQuery mobile. In my loop, I have the code below: for( var i = 0; i < imageArray.length; i++ ) { counter ++; // Create a new Image element var img = $('<img data-rel="popup" class=" ...

What is the best way to ensure your background image appears proportional?

I'm having an issue with the background image on my login page - it doesn't look like the original photo: Take a look at the screenshot to see the problem with the width of the image: https://i.sstatic.net/qb9u0.jpg Here's the HTML/jsx cod ...

How can screen readers be alerted about freshly loaded content through ajax?

Whenever a user clicks on a button, I want to add more content to the page. The new content will be visually clear, but how can I ensure that screen readers can easily access it? I've come across two best practices that suggest additional steps may b ...

Inject JSON data into an HTML page as it loads continuously

Currently in the process of developing a multi-step page using ASP.NET and C#. The initial request came from an HTML page to trigger the ASP.NET page. An important aspect I am focusing on is finding a way for the ASP.NET page to send real-time status upda ...

Styles in CSS for the first column of a table, with the exception of the first cell in

Here is the HTML code for a table: <table class='census'> <tr> <th colspan="2">My Title</th> </tr> <tr> <td colspan="2" class='chart'><SOME PIE CHART, GENERATED W ...

`Uniform background color across all pages`

My goal is to allow customers to select a color that will persist across different pages on the website. The code below shows how users can choose a color on the first page: <select id="color" style="width: 5%; height: 10%" size="5"> ...

Retrieve a specific string located within a <div> element by utilizing XPath

Below is the structure of the HTML I am working with: <div class = "something" > <div> <span>Some text</span> </div> "Automated Script" </div> Currently, I am using Selenium Webdriver for testing and ne ...

Exploring the Power of AJAX Pagination in CakePHP 3

I'm currently working on implementing ajax pagination in cakephp 3. Although I have added a simple jquery code for pagination, I'm aware that this is not the optimal solution. Below is the code that I have experimented with: $('document&apo ...

Implementing a hamburger menu across various webpages

I recently followed a tutorial on adding a hamburger menu from this YouTube video. The menu works perfectly on the INDEX.html page, but when I try to add the same code to other pages like "contact" or "about", none of the menu features seem to work. I rea ...

"Creating a user-friendly interface design with two separate divs, each showcasing a

I am looking to create a UI/UX design that includes a logo and two menus, one for language change and one for navigation. https://i.sstatic.net/Tb6zc.png My current approach involves two div elements: 1. The first div contains the image and language men ...

What is the best way to incorporate this design utilizing the Bootstrap Grid system?

I am trying to create a grid structure using the code below: <div class="col-lg-8"> <div class="col-lg-6"> <div class="panel panel-primary"> <!-- Default panel contents --> <div class="panel- ...

Tips for choosing input content when there is a single error class

I'm trying to determine if there is exactly one div with an error class. If so, I want to use the .select() method to select the content of the corresponding input (within that input's parent div). Any ideas on how to achieve this? This is my a ...

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

Check out this awesome tip: Enhancing your styles with Regex for maximum specificity detection in

I am facing an issue with Style Lint where I have configured the selector-max-specificity to 0,2,0 and ignored certain rules like ignoreSelectors: [":focus", ":hover", etc..] However, I am unable to ignore the :not(.myClass) selector as desired. For inst ...

Creating a Dynamic Clear Button for a Text Area in Angular

Working on my Angular application, I have implemented a form with a textarea element. My goal is to incorporate a clear button inside the textarea element that should: Appear only when the textarea is focused Disappear when the textarea is out of focus ( ...

What is the best way to centrally align menu items using CSS?

I attempted to center the menu items in CSS. The navigation bar spans the entire width of the screen, which is the desired outcome. However, I am struggling to get the menu items to be centered relative to the screen. Below is the CSS code that I am curre ...