bootstrap slider isn't working using dom

zecao lu

I want to dynamically create bootstrap slider, so I decided to using dom to create one.

Now my js code is like this:

  const newItem = document.createElement('input');
  newItem.setAttribute('id', 'slider');
  newItem.setAttribute('type', 'text');
  newItem.setAttribute('data-provide', 'slider');
  newItem.setAttribute('data-slider-min', '1');
  newItem.setAttribute('data-slider-max', '3');
  newItem.setAttribute('data-slider-step', '1');
  newItem.setAttribute('data-slider-value', '1');
  newItem.setAttribute('data-slider-tooltip', 'hide');

But it only displays a text box, I use Chrome dev tool to inspect the element, it looks like this:

<input id="slider" type="text" data-provide="slider" data-slider-min="1" data-slider-max="3" data-slider-step="1" data-slider-value="1" data-slider-tooltip="hide">

My configuration seems right because I create a slider successfully using html, like this:

    <input
        type="text"
        data-provide="slider"
        data-slider-min="1"
        data-slider-max="3"
        data-slider-step="1"
        data-slider-value="3"
        data-slider-tooltip="hide"
    >

In bootstrap-slider, it said:

Create an input element with the data-provide="slider" attribute automatically turns it into a slider.

So the problem seems like that bootstrap-slider did not interpret my attribute.

Abhishek Raj

I guess you need to initialize your slider something like this (if you're not using jQuery):

// Instantiate a slider
var mySlider = new Slider(newItem, {
   // initial options object
   step: 1,    
   min: 1,
   max:3,
   value: 1,
   tooltip: 'hide'
});

So, In your case, if you also want to build a dynamic input and then attach a slider to it, your whole code would look like this:

// Create an input element dynamically
var newItem = document.createElement('input');

const body = document.querySelector('body');
// Add your new created element to body (or any other div)
body.appendChild(newItem);

// Instantiate a slider
var mySlider = new Slider(newItem, {
   // initial options object
   step: 1,    
   min: 1,
   max:3,
   value: 1,
   tooltip: 'hide'
 });

Here I built a live demo for you: https://jsfiddle.net/abhishekraj007/hdr253xb

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why isn't this bootstrap carousel slider sliding?

Bootstrap - Modal isn't working

Why Navbar Collapse isn't working for me using Bootstrap?

I am using bootstrap to create a navbar but it isn't working,however other classes of bootstrap are working fine

Dropdown button isn't working with bootstrap 4

Custom Bootstrap carousel with thumbnails isn't working

Why isn't my bootstrap classes working?

Bootstrap dismissal isn't working in Ruby on Rails

Validation isn't working for me for a bootstrap form

Bootstrap navbar collapse isn't working

bootstrap isn't working in symfony template

Bootstrap Grid isn't working properly

Bootstrap hamburger menu isn't working on phone

React input range slider for volume isn't working

Image slider "previous" button isn't working properly

Search for DOM Element's attribute using onClick(event) isn't working

How to fix: "Using JS inside Bootstrap 4's div and Div isn't working but JS works"

My JavaScript image gallery slider's right slider isn't working and my left one is

Using setBounds or setLocation on JTextArea isn't working

Update isn't working using with clause

Register using PHP isn't working

My loop isn't working(using feof)

Can't figure out why Bootstrap Scrollspy isn't working

Bootstrap Scrollspy isn't working, doesn't scroll

Slick slider not working with bootstrap

Bootstrap : Slider not working

Bootstrap- jQueryvalidator plugin isn't working with modals

Responsive navigation menu with React and Bootstrap - hamburger isn't working

A {wrap}bootstrap navbar in Aurelia just isn't working right

TOP Ranking

HotTag

Archive