0

I have a JQuery UI Slider and by moving it forward it's display formatted currency with RS as rupees symbol. As shown in the picture

enter image description here

The JQuery Code is given

$('#personalLoanSlider').slider({
            range: "min",
            value: 1,
            step: 25000,
            min: 200000,
            max: 4000000,
            slide: function( event, ui ) {
                $('#personalloanAMT').val(formatINR("Rs. " + ui.value ));
                $('#wordAmount').html(convertNumberToWords( ui.value) + "Rupees Only");
            }

        });

I want a functionality that if the user input their own custom values directly in to textbox then the slider will move forward or backward.

I have tried with below code but its not working

        $("#personalloanAMT").change(function () {
             var value = this.value.substring(1);
              console.log(value);
                $('#wordAmount').html(convertNumberToWords( ui.value) + "Rupees Only");
        }); 
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Niharika Parida
  • 103
  • 1
  • 1
  • 16

2 Answers2

1

You could use the input event on your input field, convert the input value to a proper number and update the slider's value.

jQuery(document).ready(function($){
var $personalLoanSlider = $('#personalLoanSlider').slider({
            range: "min",
            value: 1,
            step: 25000,
            min: 200000,
            max: 4000000,
            slide: function( event, ui ) {
                $('#personalloanAMT').val(formatINR("Rs. " + ui.value ));
                
            }

        }); 
 function formatINR(unit){
  return unit + "₹";
 }
 $(document).on('input', '#personalloanAMT', function(e) {
  var value = e.target.value;
  var validSliderValue = Number(value.replace(/\D+/g, ''));
  $personalLoanSlider.slider( "option", "value", validSliderValue );
 });
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
<div id="personalLoanSlider"></div>
<br/> 
<input id="personalloanAMT" type="text" > 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</body>
</html>

You can read more about the jQuery input event here jQuery 'input' event

here is a jsbin of the above code snippet https://jsbin.com/hupozeb/edit?html,js,output

azs06
  • 3,467
  • 2
  • 21
  • 26
0

You need to do like this, because you want to move the slider accordingly

$("#personalLoanSlider").slider("value", parseInt(value));

Additionally can be done like this

        $("#personalloanAMT").change(function () {
               var value = this.value.replace(/[^0123456789]/gi, '');
               var num = parseInt(value, 10);
                alert(value);
              $("#personalLoanSlider").slider("value", parseInt(num));
              $('#personalloanAMT').val(formatINR("Rs. "+num));
              $('#wordAmount').html(convertNumberToWords(num) + "Rupees Only");   

        });
Sashi
  • 686
  • 8
  • 21