Managing numerous range sliders in a Django form

My Request: I am looking to have multiple Range sliders (the number will change based on user selections) on a single page. When the sliders are moved, I want the value to be updated and displayed in a span element, as well as updating the model.

The Issue: I am struggling with changing the values of sliders when there are multiple sliders created using a for loop. The span element does not update, and the value of the slider does not change when I move the handle.

HTML:

<form method="post">
    {{ formset1.management_form }}
    {% for form in formset1 %}
        <div class="custom-slider-container">
            <label>{{ form.name.value }}</label>
            <span id="demo-{{ forloop.counter0 }}" class="range-val">0</span>
            {{ form.weight }}
        </div>
    {% endfor %}
</form>

Views.py:

initial = Driver.objects.filter(simulation=chosenSim, type='KEY_OUTPUT')
formset = sliders_formset(request.POST or None, queryset=initial)
    if 'sliders_confirm' in request.POST:
        if formset.is_valid():
            instances = formset.save(commit=False)
            for form in instances:
                form.save()
        return HttpResponseRedirect(request.path_info)

Forms.py

sliders_formset = modelformset_factory(Driver, fields=['name', 'weight'], extra=0,
                                   widgets={'weight': RangeInput(attrs={'class': 'custom-slider', 'max': 20})})

If you require any additional information, please let me know!

Answer №1

My issue was resolved using the "each" method!

<script>
    var sliderFunction = function(){
        var sliderDiv = $('.custom-slider-container'),
        rangeSlider = $('.custom-slider'),
        valueLabel = $('.range-val-label');

        rangeSlider.each(function(){
            var rangeValue = $(this).attr('value');
            var rangeValue = Math.trunc(rangeValue);
            $(this).prev().html(rangeValue);
        });

        sliderDiv.each(function(){

            valueLabel.each(function(){
                var labelValue = $(this).next().attr('value');
                $(this).html(labelValue);
            });

            rangeSlider.on('input', function(){
                $(this).prev(valueLabel).html(this.value);
            });
        });
    };
sliderFunction();
</script>

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

Replace the ngOnDestroy method

Is there a way to complete an observable when the ngOnDestroy is triggered? I'd prefer not to create new child components when dealing with just one component instance. I attempted to override ngOnDestroy by modifying the function in the component&apo ...

Having trouble with the lodash find function in my Angular application

npm install lodash npm install @types/lodash ng serve import { find, upperCase } from 'lodash'; console.log(upperCase('test')); // 'TEST' console.log(find(items, ['id', id])) // TypeError: "Object(...)(...) is un ...

What is the best way to put together the perfect attire for Threejs shaders?

I have been experimenting with using Three.js' built-in ShaderChunks for implementing lighting and fog effects, and I decided to start by mimicking a setup from one of the ShaderLib shaders. Initially, I utilized the following code snippet: customMat ...

Route creation unsuccessful while attempting to both download a file and send a response message, resulting in the error "Headers cannot be set after they have been sent to the client."

I'm currently working on setting up a route that will allow users to download a file and receive a response message in the HTML. I attempted to use the download function from express JS, but encountered some issues. Here is the code I am using: route ...

While Advanced REST Client successfully retrieves an infinite JSON file from the AngularJS REST service, Postman encounters an error with the code ERR_INCOMPLETE_CHUNKED_ENCODING

I've encountered a peculiar issue. When making a REST call in Angular (using an app built with Ionic v1) to a Java endpoint, something goes awry and Chrome throws the following error: https://i.sstatic.net/1sxp7.png The code of interest is this Angul ...

Extract information from page 1 to page 2 by utilizing ajax

Greetings, I am currently facing an issue with parsing data using the href get method to retrieve PHP by ajax on another page. Is this achievable? By the way, my framework of choice is CODEIGNITER. This is my HTML href code snippet: <a href="'.b ...

Transmit JSON object and text parameters at the same time

Can I post a JSON object and a string parameter to an MVC3 controller method using jQuery? This is the controller function I have: public void myFunction(List<myObject> obj, string myStringparameter) Here is my JavaScript function (without the str ...

Stylish Tabbed Design

Imagine I have this html and css snippet h1{ color: rgb(61, 133, 200); font-size: 3em; margin-left: 0px !important; } h1 ~ *:not(h1) { margin-left: 0px; } h2{ font-size: 1.8em; margin-left: 40px !important; } h2 ~ *:not(h2) { ...

Encountering a difficulty in sending data to the server through ajax with Cordova

I am currently working on a Phonegap Cordova project and I'm trying to send data to the server using AJAX but I'm encountering an error. Here is an example of my code: $(document).ready(function() { $('#frm').submit(function() ...

What steps can I take to prevent a css-generated svg file from occupying more space than the original image? My wave appears to be 239px tall, while the svg file is a massive 1500px in height

I utilized an online tool to generate custom SVG CSS at this link: Upon implementing it on my webpage, the rendering seems correct in terms of height and width. However, upon inspecting the page, I noticed that the SVG file dimensions are 1512px by 1512px ...

The error message "gulp error - _.flattenDeep is not a function when using gulp.watch" is indicating

Whenever I run a watch task, why am I encountering the TypeError: _.flattenDeep is not a function error? Below is the content of my gulpfile.js : var gulp = require('gulp'); var sass = require('gulp-sass'); var sourcemaps = require(&a ...

The textbox is unable to receive input from the pop-up window

Whenever I click on a text-box, a popup window opens up. However, the value is not inserted in the text-box when I select any radio option in the popup. Can you identify where the problem lies? Here's the text-box: <tr><td> Work </td& ...

Footer not sticking to bottom using bootstrap version 5.2.2

I am currently working on a project using Laravel Inertia Vue and bootstrap 5.2.2 as part of my dependencies. After loading the css to the tag, I attempted to create a sticky footer. However, I found that the footer is not sticking to the bottom as expect ...

Developing asynchronous DOM functions for optimal performance

Imagine having a large amount of data that needs to be processed. In this scenario, the processing must happen on the client side rather than the server side. The data processing involves iterating through each element in the data set: for element in data ...

Loading datatables with ajax in Liferay portlet when searching

Having an MVC Liferay portlet, I am utilizing serveResource() to handle AJAX calls in JSP The issue at hand is that I would like to trigger an AJAX function to populate datatables upon clicking the search button. I have the following code set up: <po ...

What is preventing me from using JavaScript to remove this class?

Struggling to implement a skeleton loading screen with CSS classes and JavaScript. The idea is to apply the 'skeleton' class to elements, style them accordingly, then remove the class using a timeout set in JavaScript. However, I'm encounter ...

Is there a method in JavaScript to prevent href="#" from causing a page refresh? This pertains to nyroModal

Is there a way to prevent <herf="#"> from causing a page refresh? I am currently working on improving an older .NET web project that utilizes nyroModal jQuery for displaying lightboxes. However, when I attempt to close the lightbox, nyroMo ...

CSS and Javascript functioning correctly within internal server, but encountering issues when accessed externally

I am in the process of creating a website for a friend. The goal is to have a flashy animated style website that functions well on IOS and allows him to make changes easily. To achieve this, I am utilizing JQuery, my own javascript, and multiple css files. ...

Updating Jqplot display upon ajax call completion

Currently, I have a Jqplot set up using the AJAX JSON Data Renderer and it's functioning properly. There is a button on the page where users can input new values (updated through ajax) which are then stored in the same DB as the json data source. Whe ...

Use Python to enter a value on a webpage coded in HTML

My current project involves automating the input of values into a webpage. However, I have encountered a significant hurdle with the Mechanize library as it does not recognize the forms on my page which are <input> elements, unlike the typical form.n ...