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

The POST request from the form is returning a null value

I am currently facing an issue with using post in Express and BodyParser to insert data from a form in an EJS file into MySQL. The data keeps returning null, indicating that it is not being parsed correctly from the form to the backend. Can anyone offer as ...

React Native is facing difficulty in fetching pagination data which is causing rendering errors

Currently, I am working on fetching pagination data from an API. The process involves retrieving data from https://myapi/?page=1, then from https://myapi/?page=2, and so on. However, despite following this logic, I encountered an error that has left me puz ...

The function `req.on('end', callback)` in Node.js is not functioning as expected

I am currently working on building a proxy using nodejs. The syntax I am using for sending and receiving https requests and responses works well, but in my project, I have noticed that the response is sometimes larger than expected. This results in req.on( ...

What is the best way to create separation between the href attribute of an a tag and the

Currently, I am utilizing Material UI in React and encountering an issue with the spacing between an <a tag and some text both before and after it. The lack of space is causing them to merge together. <Typography style={{ fontSize: 14, textAlig ...

Ways to extract metadata from a given URL

Recently, I've delved into the world of JavaScript and I'm looking to extract metadata from a given URL. Whenever a URL is entered into an input field, I want to retrieve its meta data - a simple task in HTML using JavaScript. However, every time ...

Button in Angular gets stuck when a touchscreen is long pressed

In my Angular2 application, I am facing an issue with a button when running on a Windows 10 touchscreen PC in Chrome. Normally, the button works fine and executes the click function. However, if the button is held for 1-2 seconds, it gets stuck and fails t ...

I continue to encounter an error every time I attempt to place an HTML nested div on a separate line

When I structure the HTML like this, I don't encounter any errors: <div class="game-card"><div class="flipped"></div></div> However, if I format it differently, I receive an error message saying - Cannot set property 'vi ...

How to automatically refresh a page in AngularJS after a POST request

After making a POST request, I attempted to use $state.reload in my controller to refresh the page. However, it is not updating the data on my page after registering the form. I have to manually refresh the page to see the correct data. .controller(' ...

Tips for utilizing pre-installed validation regulations in VeeValidate 4?

Currently transitioning from Vue 2 to Vue 3, I am interested in utilizing VeeValidate 4 for my validations. However, it seems that the documentation does not provide any information on using built-in rules such as min, max, alpha_spaces, etc. Do I need to ...

Having trouble with Firebug throwing a '$(' error message?

I'm encountering a peculiar issue in Firebug that I'm not experiencing in Webkit. The error being displayed is '$(' in Firebug. Here is the code that seems to be causing this problem: $.getScript("http://kylehotchkiss.com/min/?g=packa ...

If you are having trouble with jQuery AJAX dataType:"json", try using Array instead

Initially, I attempted to send a JavaScript JSON Object to a PHP page but encountered difficulties in doing so. The code snippet below was used for testing: var testobj = { "test1":"test1data", "test2":"test2data", "test3":"test3data" }; alert(testo ...

Unable to assign focus to textbox

In my chrome extension, I have implemented two text boxes - one for entering a URL and the other for LDAP. Upon clicking on the extension icon, a popup page opens where I automatically fill the URL text box with the current page's URL using the code s ...

How can I set up an automatic email alert to be sent on a specific date using PHP or jQuery? Can you provide

I am developing a portal where I need to send follow-up messages to clients. To achieve this, I have to set up alert emails based on dates stored in the database. When the specified date approaches, an email will be automatically sent out. <div clas ...

Issues with div misalignment during jQuery animation

I have successfully created an animation where a child div inside a parent div moves up by -60px when the mouse enters the parent div, and returns to its original position when the mouse leaves. However, I am facing an issue where if the mouse moves direct ...

When the web browser page is zoomed out (Ctrl -), elements do not appear to float

Can you test resizing the window to the minimum (CTRL-)? Here's the link: http://jsfiddle.net/Zty9k/13/ This is the HTML code: <ul> <li><img src="http://i.imgur.com/PBuDAFH.gif"></li> <li><img src="http://i.i ...

How to locate a div element using CSS selector in Selenium WebDriver with Ruby

My experience with CSS selectors in selenium webdriver has been positive, but I am facing difficulty in locating the element within the following div. <div class="class1 class2" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_78891a3 ...

How can I modify the text color of the Navbar-Brand class in Bootstrap NavBar?

I have been experimenting with various options to change the text color of the `navbar` and specifically the `navbar-brand` item. However, my `.navbar-brand {color: purple;}` CSS class doesn't seem to be working. Can someone please assist me in determ ...

Enhancing Typography in Material UI with Custom Breakpoints in React CustomThemes

Currently, I am utilizing material UI and React to develop a single-page application (SPA). However, I have encountered an issue with ensuring that my pages are responsive for smaller screen sizes. To address this, I have been manually adding fontSize: { x ...

Reposition div when clicked

I have encountered a challenge where I am unable to perform a small task. My goal is to have the position of "div1" change upon clicking on "div2", taking into account that "div2" is nested inside "div1". Additionally, when clicking on "div2" again, "div1" ...

AWS Cognito - ECS Task Fails to Start

I'm facing an issue with using JavaScript to execute a task in ECS Fargate. AWS suggested utilizing Cognito Identity Credentials for this task. However, when I provide the IdentityPoolId as shown below: const aws = require("aws-sdk"); aws.co ...