0

I have a button which is like below and a jQuery which increase the size of the slider perfectly:

<button id='plus' type="button">+</button>

$('#plus').click(function() {
     s.slider('value', s.slider('value') + s.slider( "option", "step" ) );   
 });

But I would like to change the size up as long as the mouse is down over the button. I tried this but it didn't do the job! (it is actually acting like click event).

$('#plus').mousedown(function() {
    s.slider('value', s.slider('value') + s.slider( "option", "step" ) );   
});

How can I do this in jQuery?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

3 Answers3

1

i just understood that you want something to continuously happen while the button is pressed down.

see this answer,

the main idea is to start an interval when button is down, which will do the update on every round, and when the button is released, this interval will stop.

for example:

var timeout, clicker = $('#plus');

clicker.mousedown(function(){
    timeout = setInterval(function(){
        // Do something continuously 
        s.slider('value', s.slider('value') + s.slider( "option", "step" );
    }, 500);

    return false;
});

$(document).mouseup(function(){
    clearInterval(timeout);
    return false;
});

hope that helps.

Community
  • 1
  • 1
geevee
  • 5,411
  • 5
  • 30
  • 48
1

Try

$('#plus').bind('mousedown', function() { s.slider('value', s.slider('value') + s.slider( "option", "step" ) );
});

OR

$('#plus').on('mousedown', function() { s.slider('value', s.slider('value') + s.slider( "option", "step" ) );
});
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
0

as bind is deprecated using on should be the better solution:

$('#plus').on('mousedown', function() {
    s.slider('value', s.slider('value') + s.slider( "option", "step" ) );   
});
roman
  • 11,143
  • 1
  • 31
  • 42