Sunday, November 27, 2011

jQuery Mobile: Set value of Slider using JavaScript code dynamically

jQuery Mobile: Set value of Slider using JavaScript code dynamically

To set value of Slider using JavaScript code by calling $("#slider").val(0).slider("refresh"). Remember to include ".slider("refresh")", otherwise the slide may not be updated to the new value.

jQuery Mobile: Set value of Slider using JavaScript code dynamically

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()" />
<input type="button" value="Set Slider to 0." onclick="minbutton_clicked()" />
<input type="button" value="Set Slider to 100." onclick="maxbutton_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);
}

function minbutton_clicked(){
$("#slider").val(0).slider("refresh");
}

function maxbutton_clicked(){
$("#slider").val(100).slider("refresh");
}
</script>

</body>
</html>



next:
- jQuery Mobile: get max and min of Slider using attr()

1 comment:

  1. Thank you. I posted a link-back to this page from the jQuery forum because your script is the one that worked for me.

    https://forum.jquery.com/topic/slider-moving-slider-back-to-beginning-slider-reset-method

    ReplyDelete