ekbayrak2 ekbayrak2 - 8 days ago 6
jQuery Question

Jsfiddle demo to html page

I have this jsfiddle I made http://jsfiddle.net/yd6xmtvq/, and want to implement this on a html page. I included the css files of the bootstrap in the html, and they are located in the same directory as tht html file. This is how my code is setup:

<html>

<head>

<!-- Bootstrap Core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<link href="bootstrap-slider.css" rel="stylesheet">
<script src="bootstrap-slider.js"></script>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>

var minSliderValue = $("#ex1").data("slider-min");
var maxSliderValue = $("#ex1").data("slider-max");

$('#ex1').slider({
value : 0,
formatter: function(value) {
return 'RAM: ' + value + 'GB';
}
});

$('#ex2').slider({
value : 0,
formatter: function(value) {
return 'Disk Space: ' + value + 'GB';
}
});

$('#ex3').slider({
value : 0,
formatter: function(value) {
return 'CPU : ' + value + ' Cores';
}
});

// If You want to change input text using slider handler
$('.slider').on('slide', function(slider){

var val1 = $("#ex1").val();
var val2 = $("#ex2").val();
var val3 = $("#ex3").val();
$("#inputValue").val(parseInt(val1)+ parseInt(val2)+ parseInt(val3));

});

// If you want to change slider using input text
$("#inputValue").on("keyup", function() {
var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
this.value = val > maxSliderValue ? maxSliderValue : val;
$('#ex1','ex2','ex3').slider('setValue', val);
});


</script>


</head>
<body>


<div class="wrapper">
<input class="slider" id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
<hr />

<input class="slider" id="ex2" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
<hr />
<input class="slider" id="ex3" data-slider-id='ex1Slider3' type="text" data-slider-min="0" data-slider-max="4" data-slider-step="1" />

<hr />

<input type="text" class="form-control" id="inputValue" value="0" readonly />


</div>
</body>
</html>


Any help is appreciated

Answer

You have to correct the following three things.

  1. You haved added jQuery after bootstrap-slider.js. JQuery should be added before the bootstrap-slider.js library.
  2. You have the scripts inside the <head> tag which should be down to all the html elements, otherwise should be enclosed with $(document).ready.
  3. Also you have missed a minor css style to include .wrapper { }

Working copy:

<html>

<head>

    <!-- Bootstrap Core CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

 <link href="http://seiyria.com/bootstrap-slider/css/bootstrap-slider.css" rel="stylesheet">
  
  <style>
    .wrapper {
  padding: 50px;
  margin-top: 25px;
}

  </style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://seiyria.com/bootstrap-slider/js/bootstrap-slider.js"></script>




</head>
<body>


<div class="wrapper">
  <input class="slider" id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
      <hr />

  <input class="slider" id="ex2" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
       <hr />
      <input  class="slider" id="ex3" data-slider-id='ex1Slider3' type="text" data-slider-min="0" data-slider-max="4" data-slider-step="1" />

       <hr />

      <input type="text" class="form-control" id="inputValue" value="0" readonly />


      </div>
  
  <script>

var minSliderValue = $("#ex1").data("slider-min");
var maxSliderValue = $("#ex1").data("slider-max");

$('#ex1').slider({
    value : 0,
    formatter: function(value) {
        return 'RAM: ' + value + 'GB';
    }
});

$('#ex2').slider({
    value : 0,
    formatter: function(value) {
        return 'Disk Space: ' + value + 'GB';
    }
});

$('#ex3').slider({
    value : 0,
    formatter: function(value) {
        return 'CPU : ' + value + ' Cores';
    }
});

// If You want to change input text using slider handler
$('.slider').on('slide', function(slider){

  var val1 = $("#ex1").val();
  var val2 = $("#ex2").val();
  var val3 = $("#ex3").val();
  $("#inputValue").val(parseInt(val1)+ parseInt(val2)+ parseInt(val3));

});

// If you want to change slider using input text
$("#inputValue").on("keyup", function() {
    var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
    this.value = val > maxSliderValue ? maxSliderValue : val;
    $('#ex1','ex2','ex3').slider('setValue', val);
});


</script>
</body>
</html>

Comments