I have a jquery mobile slider on a simple page. When I drag the slider, the value updates as expected in the textbox. I have looked but cannot find where this logic is happe
Shortest yet and functional.
$('body').change(function() {
var slider_value = $('#slider-1').val();
if(slider_value == 4000){
alert("Holy smoke!");
}
}
I highly suggest checking out w3schools tutorial on this.
http://www.w3schools.com/jquerymobile/jquerymobile_events_intro.asp
The crucial piece of code that you are missing is:
$(document).on("pageinit","#pageone",function(){
// PLACE ALL YOU EVENTS CODE HERE e.g.
$( "#slider" ).on( 'slidestop', function( event ) {
alert ('hi');
});
}
Make sure your content is prefixed with:
<div data-role="page" id="pageone">
<div data-role="content">
<input type="range" name="slider" id="slider" value="10" min="0" max="100">
</div>
</div>
The documentation appears to be lacking. After searching for a long time I found that to get the new value in the event handler, something like $(this).slider().val()
is needed (the extra slider()
being the part omitted almost everywhere):
$("#div-slider").change(function() {
var slider_value = $(this).slider().val();
// do something..
});
The problem with your code is that the javascript binding the event of change to the elements is evaluated before they were actually created in the html, thus no element get binded to the change event as you specified.
Try this:
$('#slider-1').live('change', function(){
doYourStuffHere();
});
Briefly, live means that JQuery will keep on binding events to elements matching the specified selector even if they were not present at time of evaluating the JS for binding, so this code will bind all present and future html elements which match #slider-1 selector with the proper change callback.
Another alternative is to use JQuery 'on' binding which acts little bit differently, but can still do the job. Have a look at the documentation for 'on' here.