javascript - jQuery: change the value of input range continuously with mousedown event -
i trying change value of input range continuously until mousedown on button
code tried
html
<input type="range" id="points" value="50" min="10" max="100" step="1" /> <input type="button" id="plus" value="+" /> <input type="button" id="minus" value="-" />
jquery
$('#plus').on('mousedown', function() { oldvalue = parseint($('#points').val()); $('#points').val(oldvalue + 5).change(); console.log($('#points').val()); }); $('#minus').on('mousedown', function() { oldvalue = parseint($('#points').val()); $('#points').val(oldvalue - 5).change(); console.log($('#points').val()); });
jsfiddle
code value of input range changed once, requirement keep changing value untill mousedown
thanks in advance help
your problem event triggered once... should sure (until event continues) code periodically executed. idea can start periodic timer on "mousedown" event using settinterval() function, , stop on "mouseup" event clearinterval(). in example decleared interval
"global" variable. executed function 1 time before setting interval, otherwhise on click doesn't executed.
hope helps.
var interval; $('#plus').on('mousedown', function() { starttime(true); // work on click interval = setinterval(function(){starttime(true);}, 500); }); $('#minus').on('mousedown', function() { starttime(false); // work on click interval = setinterval(function(){starttime(false);}, 500); }); $('.rangemover').on('mouseup', function() { clearinterval(interval); }); function starttime(plus) { var oldvalue = parseint($('#points').val()); if (plus) { $('#points').val(oldvalue + 5).change(); } else { $('#points').val(oldvalue - 5).change(); } console.log($('#points').val()); } <input type="range" id="points" value="50" min="10" max="100" step="1" /> <input type="button" id="plus" class="rangemover" value="+" /> <input type="button" id="minus" class="rangemover" value="-" />
be aware of html modification too.
ps: http://jsfiddle.net/uu9o1vam/ if prefer.
Comments
Post a Comment