Reset and recreate a bootstrap-slider

I have implemented the Bootstrap-slider plugin from https://github.com/seiyria/bootstrap-slider using version 10 (also tested with v11) in my code:

/* Enhancing style - Bootstrap slider used */
$('.bs_sliders').each(function () {
    var id = '#' + $(this).attr('id');

    if (typeof window[id] !== 'undefined') {
        window[id].slider('destroy');
        delete window[id];
    }

    // Using ID selector as .on requires it
    window[id] = $(id).slider({
        formatter: function (value) {
            return value;
        },
    }).on('change', function (ev) {
        $(this).closest('li').find('.rangeUpdate').val($(this).val());

        $(this).closest('li').find('.continuing_eval').removeClass('continuing_eval');

        autoSave();
    })
})

My goal is to destroy and re-initialize a slider if it already exists. However, I noticed that when the range values are set from 0-100, the slider only goes up to 10 even though the input data is correct:

<input type="text" name="54_slider" value="22" class="bs_sliders elRes" id="54_slider" data-slider-id="54_slider"
    data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="22"
    data-slider-handle="custom"
    data-slider-rangeHighlights='[{"start":25,"end":26,"class":"grey_line"},{"start":50,"end":51,"class":"grey_line"},{"start":75,"end":76,"class":"grey_line"}]' />
<input type="text" value="22" class="rangeUpdate" data-toggle="popover" data-trigger="manual"
    data-content="The permitted value range is 0-100" style="--my-content:'Undefined';" />

Furthermore, the custom elements like divider lines are not being recognized either. How can I ensure that all custom elements and attributes are picked up when a bs-slider is reinitialized?

Answer №1

The secret trick is to dismantle it yourself ... The process involves duplicating the original element, retrieving its value, eliminating the element, reattaching the duplicate, and then updating the slider's value. It may seem like a lengthy process and one would assume that simply "destroying" it would suffice, but apparently not.

$('.bs_sliders').each(function(){
    var id = $(this).attr('id');

    // Detach and Reattach slider by cloning the current element
    if (typeof my_sliders[id] !== 'undefined'){
        delete my_sliders[id];

        var setVal = $(this).attr('value');

        // Find parent of the current element for new element placement
        var our_parent = $(this).parents('.slide_slidecontainers');

        // Clone the element
        var new_elem = $(this).clone();

        // Remove the current input element and rebuild it
        $(this).remove();

        $(our_parent).find('.slider').remove();

        $(our_parent).append($(new_elem));

        use_elem = $(new_elem);
    // Just attach using the existing element
    } else {
        use_elem = $(this);
    }

    my_sliders[id] = $(use_elem).slider();

    if (typeof setVal !== 'undefined'){
        my_sliders[id].slider('setValue',setVal);
    }

    my_sliders[id].on('change',function(ev){
        $(this).closest('li').find('.rangeUpdate').val($(this).val());

        $(this).closest('li').find('.continuing_eval').removeClass('continuing_eval');

        autoSave();
    })
})

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 issue of JQuery Datatables columns overlapping arises during the application of column filtering

I have integrated a Jquery DataTable inside a Bootstrap division. The Bootstrap row is split between the DataTable and a Chartjs chart. Initially, everything displays correctly without any overlapping. However, when I implemented code to enable column fil ...

Navigate within the div by scrolling in increments of 100%

I am facing an issue with a div that contains multiple children set to 100% height. My goal is to scroll exactly the height of one child (which is also 100%) on each scroll. However, I am struggling to prevent scrolling multiple steps at a time. I have tri ...

Tips for effectively changing Observable data into an Array

I am currently attempting to transform an Observable into an Array in order to loop through the Array in HTML using ngFor. The Typescript code I have now is causing some issues. When I check the testArray Array in the console, it shows up as undefined. it ...

How can I validate HTML input elements within a DIV (a visible wizard step) situated within a FORM?

I recently made a decision to develop a wizard form using HTML 5 (specifically ASP.NET MVC). Below is the structure of my HTML form: @using (Html.BeginForm()) { <div class="wizard-step"> <input type="text" name="firstname" placeholder ...

What is the best way to add portfolio projects into a React application?

I am currently building a portfolio using react. I am having trouble navigating to my projects through localhost:3000. The projects are stored in my includes folder and are not created with react, so it is different from using XAMPP as my server. My file ...

Avoid opening the page when attempting to log in with jquery, ajax, and php

I am facing an issue with my code. I have a file named "index.html" which contains a login form. Another file called "dash.js" retrieves the username and password from the login form and redirects to "connectdb.php" to check the login credentials with the ...

Activate the places_changed event in Google Maps by clicking the submit button

I would like to activate my placed_changed function when a submit button is clicked. Here's what I have so far: I've set up a searchbox using google.maps.places.SearchBox, and when I press enter while the search box is in focus, it triggers the e ...

Having trouble importing SVG as a React component in Next.js

I initially developed a project using create-react-app with Typescript, but later I was tasked with integrating next.js into it. Unfortunately, this caused some SVGs throughout the application to break. To resolve this issue, I implemented the following pa ...

Tips for utilizing the Struts2 submit tag as a button without triggering form submission

I am currently utilizing the Struts2 framework in my application, and I have a button on my JSP page. Here is the code for the button: <s:submit type="button" name="btnSave" /> However, I want this button to act like a normal HTML button, meaning i ...

Scrapy-splash seems to be encountering difficulties in extracting elements during the scraping process

My attempt to extract data from this Why is Splash not working here? Does anyone have a solution for me? Your assistance will be highly appreciated! ...

Validating Object Visibility Using Three.js and Sphere Intersection

I have a spherical globe adorned with pins on its surface, each pin representing an object and labeled accordingly with DOM elements. These labels are calculated based on the positioning of the pin in the 2D world. The issue arises when the pins move behi ...

The error message "TypeError: undefined is not a function when trying to access a resource

I searched online before asking here, but couldn't find a solution. I'm attempting to call a REST service using ngResource, but my app UI isn't launching due to an error: 05-13 02:16:08.011 2402-2402/com.app.mvalt I/chromium: [INFO:CONSOLE ...

How can I remove just one specific card from an array when transferring it onto another card, without deleting all of them?

I'm currently working on a project where I am mapping out an array to create cards with object properties. However, I am facing a problem where clicking on the "Not Interested" button (which acts as a delete post button) is causing all the posts to be ...

Transfer a session ID cookie generated by express-session to a React front end on a different domain

I am currently developing a web application. The back end of my app is built using Node.js and Express.js, with a specific focus on utilizing the express-session module for implementing session-based authentication to maintain user logins. I understand tha ...

Utilizing HTML onClick to pass a PHP variable as a parameter

After going through many similar examples, I am still struggling to get this code working. This is what I currently have: echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;"> <button class="btnT ...

Merging a variable and its corresponding value in JavaScript

I am attempting to achieve a similar functionality in Angular javascript (with simplified code): var modelName = "date"; if (attrs.hasOwnProperty('today')) { scope.modelName = new Date(); } In the scenario above, my intention is for scope.m ...

Trouble encountered when submitting a form with Reactjs

I am currently working on Reactjs (Nextjs) and I am facing some issues while trying to submit a form. Can anyone provide me with guidance on how to properly submit form data? Below is the snippet of my current code: export default function Home() { ...

Maximizing Memory Usage in Objects

Which of these coding patterns is considered to be more efficient? Pattern 1: var jso = new Class({ initialize: function () { // implementation }, hiliteField: function () { // implementation } }); Pattern 2: var jso = new Class({ ...

showing 2 items in a row on mobile devices and 4 items in a row on larger screens with the help of bootstrap

I am currently constructing a static e-commerce platform using bootstrap. My goal is to have 2 products displayed in a row when viewed on a phone, and 4 products showcased in a row for desktop or iPad users. <div class="container"> <div class=" ...

Showing just the keys of a JavaScript object array using Angular

Check out this live code example on JSbin Can you show only the key from expression syntax in Angular? Specifically, the key being the word "page". And is it possible to do this from an array (even though they are the same word)? HTML <body ng-app="a ...