ekbayrak2 ekbayrak2 - 9 days ago 6
jQuery Question

display text on page instead of input box

I have this piece of code that has 3 sliders and displays the total of those 3 values in an input box, what I would like to do is display the value just plain on the page. So it should have the 3 sliders on top and have the value of those 3 display below that, no inputbox. This is my code:


<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="ram" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
<hr />

<input class="slider" id="diskSpace" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
<hr />
<input class="slider" id="cpu" 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 = $("#ram").data("slider-min");
var maxSliderValue = $("#ram").data("slider-max");

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

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

$('#cpu').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 ram = $("#ram").val();
var diskSpace = $("#diskSpace").val();
var cpu = $("#cpu").val();
$("#inputValue").val(parseInt(ram)+ parseInt(diskSpace)+ parseInt(cpu));

});


// 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;
$('#ram','diskSpace','cpu').slider('setValue', val);
});


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

Answer

Simply add a div like this:

<div id = "info"></div>

And in your javascript code do this:

$("#info").html(parseInt(ram)+ parseInt(diskSpace)+ parseInt(cpu));

Here's a working solution. Hope it helps!

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

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

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

$('#cpu').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 ram = $("#ram").val();
  var diskSpace = $("#diskSpace").val();
  var cpu = $("#cpu").val();
  $("#inputValue").val(parseInt(ram)+ parseInt(diskSpace)+ parseInt(cpu));
  $("#info").html(parseInt(ram)+ parseInt(diskSpace)+ parseInt(cpu));

});


// 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;
    $('#ram','diskSpace','cpu').slider('setValue', val);
});
.wrapper {
  padding: 50px;
  margin-top: 25px;
}
#info{
	font-size: 30px;
	text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<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>
<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">
<div class="wrapper">
  <input class="slider" id="ram" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" />
      <hr />

  <input class="slider" id="diskSpace" data-slider-id='ex1Slider2' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="10" />
       <hr />
      <input  class="slider" id="cpu" 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>
<div id = "info"></div>

Comments