Sunday, November 27, 2011

jQuery Mobile: Read value of Slider

To get value of Slider in jQuery Mobile, we can read $("#slider").val().

jQuery Mobile: Read value of Slider

example:
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, height = device-height" />
<title>jQuery Mobile: Slider</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
</head>
<body>

<div data-role="page" id="home">
<div data-role="header">
<h1>Slider</h1>
</div>
<div data-role="content">
<p>Hello ALL</p>
<p>jQuery Mobile: Slider</p>
<div data-role="fieldcontain">
<label for="slider">Slider:</label>
<input type="range" name="slider" id="slider" value="0" min="0" max="100" />
<input type="button" value="Read Slider" onclick="button_clicked()" />
</div>
</div>
<div data-role="footer">
<h4><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></h4>
</div>
</div>

<div data-role="page" id="dialog">
<div data-role="header">
<h1>mobile-web-app</h1>
</div>
<div data-role="content">
<p>It's a Dialog</p>
</div>
<div data-role="footer">
<h4><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></h4>
</div>
</div>

<script>
function button_clicked(){
var SliderValue = $("#slider").val();
alert("Slider value = " + SliderValue);
}
</script>

</body>
</html>



next:
- jQuery Mobile: Set value of Slider using JavaScript code dynamically

3 comments:

  1. but, how do you get it when the slider has changed?

    ReplyDelete
  2. (answering my own question -- found here:
    http://forum.jquery.com/topic/how-get-the-value-of-slider-in-the-js-file
    the slider is ready yet when you attach events to it. So, need to wait for the page to load, then attach the change binding to it.


    $('[data-role="page"]').live('pageshow', function () {
    $("#slider").change(function() {
    var SliderValue = $("#slider").val();
    alert("Slider value = " + SliderValue);
    });
    });

    ReplyDelete