Mas Zero
Mas Zero

Reputation: 593

jQuery Mobile: how to get the change event of a range Slider?

What I am trying to do is just like the code: when the value is greater than 10, display >10, or display 1 - 10. It works fine. However, if I uncomment the jquery mobile , it does not work anymore.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
<!--<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>-->

</head>
<body>
<form>
<label for='range'>as</label>
<input id='range' type='range' onmousemove='test()'>
<p id='display'>display</p>
</form>
<script>
function test(){
  var v = $('#range').val();
  if(v <= 10)$("#display").text('1-10');
  else $("#display").text('>10');
}
</script>
</body>
</html>

Upvotes: 1

Views: 1704

Answers (2)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

I would suggest you to wrap input within a div and attach change event to that particular div, through which you can get the value of slider. Working Snippet below.

$("#slider").change(function() {
  $("#display").text($('#range').val()<=10?'1-10':'>10');
});
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<form>
  <div id="slider">
    <label for='range'>as</label>

    <input id='range' type='range' min="0" max="100" value="0">
  </div>
  <p id='display'>display</p>
</form>

Upvotes: 1

deblocker
deblocker

Reputation: 7697

There are three JQM slider events available after widget initialization:

  • slidestart
  • change
  • slidestop

To get the correct behavior, you should provide the correct JQM page structure and register the event handlers inside the pagecreate event. Below is a simple stub where you can test a JQM slider:

$(document).on("pagecreate", "#page-one", function() {
  $("#slider").on("slidestart", function() {
    var val = $(this).val();
    $("#txt1").val(val);
  });
  $("#slider").on("change", function() {
    var val = $(this).val();
    $("#txt2").val(val);
  });
  $("#slider").on("slidestop", function() {
    var val = $(this).val();
    $("#txt3").val(val);
  });
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
</head>
<body>
  <div id="page-one" data-role="page">
    <div role="main" class="ui-content">
      <label for="slider">Slider</label>
      <input type="range" name="slider" id="slider" min="1" max="100" value="1" step="1" autocomplete="off" />
        <fieldset class="ui-grid-b">
          <div class="ui-block-a">
            <label for="txt1">slidestart:</label>
            <input data-mini="true" name="txt1" id="txt1" value="" type="text">
          </div>
          <div class="ui-block-b">
            <label for="txt2">slidechange:</label>
            <input data-mini="true" name="txt2" id="txt2" value="" type="text">
          </div>
          <div class="ui-block-c">
            <label for="txt3">slidestop:</label>
            <input data-mini="true" name="txt3" id="txt3" value="" type="text">
          </div>
        </fieldset>
    </div>
  </div>
</body>
</html>

Upvotes: 2

Related Questions