Generate image results based on the values selected in a range slider and submit them

After browsing through various sliders on CodePen, I stumbled upon the Range Slider created by cturney that fits my requirements perfectly. However, I have a few tweaks in mind for this range slider. I would like to add a "Submit" button that displays each result as images instead of numerical values. Below is an image example to illustrate what I have in mind :)

https://i.sstatic.net/TRME9.jpg

Here is the code snippet that I've been working on:

// To make it easier to follow along, I have added annotations to this code snippet. Feel free to explore and check out my other pens if you find this helpful


// Let's start by setting the colors for our sliders
const settings={
  fill: '#1abc9c',
  background: '#d7dcdf'
}

// First, locate all sliders on the page
const sliders = document.querySelectorAll('.range-slider');

// Loop through the list of sliders
Array.prototype.forEach.call(sliders,(slider)=>{
  // Find the input element inside each slider and attach an event listener
  slider.querySelector('input').addEventListener('input', (event)=>{
    // Update the numeric value display
    slider.querySelector('span').innerHTML = event.target.value;
    // Apply the color fill to the slider
    applyFill(event.target);
  });
  // Apply the fill immediately
  applyFill(slider.querySelector('input'));
});

// Function to apply the color fill using linear gradient
function applyFill(slider) {
  // Convert the value into a percentage
  const percentage = 100*(slider.value-slider.min)/(slider.max-slider.min);
  // Create a linear gradient based on the percentage
  const bg = `linear-gradient(90deg, ${settings.fill} ${percentage}%, ${settings.background} ${percentage+0.1}%)`;
  slider.style.background = bg;
}
<p>Role Match</p>
<div class="range-slider">
  <input class="range-slider__range" type="range" value="4" min="0" max="10">
  <span class="range-slider__value">4</span>
</div>

<p>Availability</p>
<div class="range-slider">
  <input class="range-slider__range" type="range" value="5" min="0" max="10" step="1">
  <span class="range-slider__value">5</span>
</div>
<p>Skills Match</p>

<div class="range-slider">
  <input class="range-slider__range" type="range" value="6" min="0" max="10">
  <span class="range-slider__value">6</span>
</div> 

<p>Custom Field Match</p>

<div class="range-slider">
  <input class="range-slider__range" type="range" value="7" min="0" max="10">
  <span class="range-slider__value">7</span>
</div> 

Answer №1

This is a straightforward demonstration of the desired outcome. By choosing the first slider range as 1, 2, or 3, the image will adjust accordingly. The same can be applied based on the values of other sliders. This serves as an illustrative guide of the process. Please bear in mind that this code has not been optimized.

// Annotations have been included for easier comprehension. Best of luck with your learning journey and explore my other pens for more insights


// Let's start by defining the colors for our sliders
const settings={
  fill: '#1abc9c',
  background: '#d7dcdf'
}

// Locate all the sliders in our document
const sliders = document.querySelectorAll('.range-slider');

// Loop through the list of sliders
// ... this loop goes through each slider [slider1,slider2,slider3] and assigns them one-by-one to the variable named slider below. Each slider can then be accessed using its variable name
Array.prototype.forEach.call(sliders,(slider)=>{
  // Inside each slider, find the input element and add an event listener
//   ... the input addEventListener() looks for the input action, which could be changed to something like 'change'
  slider.querySelector('input').addEventListener('input', (event)=>{
    // 1. Set the value to the span
    slider.querySelector('span').innerHTML = event.target.value;
    // 2. Apply the fill color to the input
    applyFill(event.target);
  });
  // Apply the fill color immediately without waiting for the listener!
  applyFill(slider.querySelector('input'));
});

// This function applies the fill color to our sliders using a linear gradient background
function applyFill(slider) {
  // Convert the value into a percentage to determine its position between the min and max of our input
  const percentage = 100*(slider.value-slider.min)/(slider.max-slider.min);
  // Create a linear gradient that changes at the specified point
  // The background color will change here
  const bg = `linear-gradient(90deg, ${settings.fill} ${percentage}%, ${settings.background} ${percentage+0.1}%)`;
  slider.style.background = bg;
}
const images = [{id:1, link : "https://via.placeholder.com/150"},{id:2, link : "https://via.placeholder.com/250"},{id:3, link : "https://via.placeholder.com/350"}];
document.getElementById("form").onsubmit = function (e){
e.preventDefault();
const selectedValueOfFirstSlider = document.getElementById("firstSlider").value;
const element = images.find((el)=>{ return el.id==selectedValueOfFirstSlider});
document.getElementById("img").src=element.link;
}
<p>Role Match</p>
<form id="form">
<div class="range-slider">
  <input class="range-slider__range" type="range" id="firstSlider" value="4" min="0" max="10">
  <span class="range-slider__value">4</span>
</div>

<p>Availability</p>
<div class="range-slider">
  <input class="range-slider__range" type="range" value="5" min="0" max="10" step="1">
  <span class="range-slider__value">5</span>
</div>
<p>Skills Match</p>

<div class="range-slider">
  <input class="range-slider__range" type="range" value="6" min="0" max="10">
  <span class="range-slider__value">6</span>
</div>

<p>Custom Field Match</p>

<div class="range-slider">
  <input class="range-slider__range" type="range" value="7" min="0" max="10">
  <span class="range-slider__value">7</span>
</div>
<input type="submit" value="Submit" id="button">
</form>
<img src="https://via.placeholder.com/100" width="500" id="img">

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

Anticipated a JavaScript module script, but the server returned a MIME type of text/html as well as text/css. No frameworks used, just pure JavaScript

I have been attempting to implement the color-calendar plugin by following their tutorial closely. I have replicated the code from both the DEMO and documentation, shown below: // js/calendar.js import Calendar from '../node_modules/color-calendar&ap ...

What are the steps to implement an Express Error handling middleware in my specific scenario?

I have implemented a router middleware to validate requests and a global Error handler middleware in the server.js file to handle all errors. However, I am encountering an issue within the router while trying to implement this logic. The problem is highli ...

Comparing Mongoose and MongoDB in Node.js: Weighing the Benefits of Each

Just starting out with Node.js and noticing the multitude of libraries available to work with MongoDB. The two most popular options appear to be mongoose and mongodb. Can someone provide a comparison of the pros and cons of these extensions? Are there any ...

Tips for ensuring that a span element outside of an anchor functions as though it were inside of it

I'm facing a challenge with my markup that I need help solving. Here is the scenario: I have provided a link within an anchor tag, but I want users to be able to click anywhere on the li tag in order to be redirected to the link specified in the ancho ...

List out IP addresses in JavaScript

Is it possible to allow users to input a range of IP addresses in a specific format? 10.5.15.[22-25],10.5.16.[35-37],10.5.17.20 The desired outcome is to return an array of these IP addresses for connection checking purposes later on. 10.5.15.22 10.5.15 ...

Exploring the methods for monitoring multiple UDP ports on a single address in Node.js within a single process

I am currently working on developing a Node.js application to manage a small drone. The SDK provides the following instructions: To establish a connection between the Tello and a PC, Mac, or mobile device, use Wi-Fi. Sending Commands & Receiving Responses ...

Next.js does not support Video.js functionality when using server side rendering

I'm struggling to set up video.js in my next.js project and encountering issues. When the player is loading, it initially appears black and then disappears abruptly. A warning message in the console reads: "video.es.js?31bb:228 VIDEOJS: WARN: T ...

Encountering a "focus" error with React-Native-Phone-Input library, where the property is null

For my project, I decided to incorporate the react-native-phone-input library. Everything was going smoothly until I encountered an issue with their focus function. Initially, it worked perfectly fine, but subsequently, when I attempted to input a phone nu ...

Creating a horizontal scrollbar in columns using Bootstrap can be accomplished by adjusting the CSS properties

Having a container created with boostrap: https://i.sstatic.net/bqAuf.png The question at hand is how to implement horizontal scrolling in order to maintain the aspect ratio of the initial image within the container on phone resolutions. Here is the con ...

Utilizing the combineReducers() function yields disparate runtime outcomes compared to using a single reducer

Trying to set up a basic store using a root reducer and initial state. The root reducer is as follows: import Entity from "../api/Entity"; import { UPDATE_GROUPING } from "../constants/action-types"; import IAction from "../interfaces/IAction"; import IS ...

Use PHP to transform an array into JSON format, then access and retrieve the JSON data using either jQuery or JavaScript

I have successfully generated JSON data using PHP: $arr = []; foreach($userinfo as $record) { $arr[] = array( 'BAid' => $record->getBAid(), 'BCid' => $record->getBCid(), 'BA ...

Having trouble with the "setValue" property being undefined error. Looking for a solution to input text into a codeMirror element using Selenium WebDriver with Java

Currently, I am attempting to input text into a textarea field that is recognized as CodeMirror. Below is the code snippet I have been working with: {... WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span")); ...

Tips on incorporating the source path from a JSON file into a Vue component

Is there a way to render images if the path is retrieved from a JSON file? Typically, I use require('../assets/img/item-image.png'). However, I'm uncertain how to handle it in this scenario. Component: <div v-for="(item, index) in i ...

Encountering a Razor syntax issue when attempting to serialize an ASP.NET Model to JSON using Html.Raw

I'm experiencing a syntax error in Visual Studio 2012 when trying to execute the following line: var data = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model)); The Model being referenced is an instance of @model M ...

Tips for Waiting for Binding in an Angular 1.5 Component (No Need for $scope.$watch)

Currently, I am in the process of developing an Angular 1.5 directive and have encountered a frustrating issue related to manipulating data that is not yet available. Below is a snippet of my code: app.component('formSelector', { bindings: { ...

The form isn't accessible through a new tab

I'm having an issue with the code I've written. Even though I specified target="_blank" in the form, it's not opening in a new tab like I intended. <script> function navigate() { var webUrl = "https://website.com/search/" + document.g ...

What is the process for modifying the characteristics of an RMWC Component?

How can I dynamically change the icon attribute in my RMWC Button element when an onClick event occurs? <Button outlined icon={<CircularProgress />} onClick={(e)=> { // e.currentTarget.icon = ''; // console.log(e.c ...

Tips for expanding the IntelliSense analysis capacity of large CSS files

My CSS file is quite large at 3.5MB and Visual Studio 2022 doesn't seem to be recognizing it properly, resulting in a lack of class suggestions. Is there a method to enhance IntelliSense analysis limits and enable code completions for such sizable fi ...

Having trouble with AJAX integration when adding two products to the cart? Looking for a solution to resolve this issue?

In the cart view page, I have noticed that when there is only one product in the cart, I am able to increase and decrease the quantity by clicking on the + and - buttons. However, if I add more than one product to the cart, these buttons do not work for an ...

Utilizing WebView for Initiating AJAX Calls

One common question often asked is whether it's possible to make ajax requests using a webview. In my case, the UI will consist entirely of native Android code, but I still need to interact with the backend using ajax calls. Fortunately, I am well-ver ...