0

I've got this code here now, but the alerts still come up after you click on the input, when it doesn't have focus. I would like to get rid of the click function too, if possible, so that it only alerts when the input has focus.

$('#inputSize').click(function(){
    if ($('#inputSize').is(':focus')){
        $(document).keydown(function(e){
            if (e.keyCode == 38) { 
               alert( "up pressed" );
               return false;
            }
                if (e.keyCode == 40) { 
               alert( "down pressed" );
               return false;
            }
        });
    }
});

Thanks!

1
  • You're binding a keydown event inside a click function, which will rebind the keydown event multiple times, causing issues. What exactly is it you're trying to do, as this is probably not the way to do it. Commented Feb 14, 2013 at 15:49

4 Answers 4

2

Just bind to the keydown event on the input. There's no need to check for focus, since it's implied:

$('#inputSize').keydown(function(e) {    
    if (e.keyCode == 38) 
    { 
       alert( "up pressed" );
       return false;
    }

    if (e.keyCode == 40) 
    { 
       alert( "down pressed" );
       return false;
    }
});
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, it does. I was mor illustrating the use of is(':focus').
This is just strange? Binding the keydown event on the document, and then doing nothing unless #inputSize has focus. The correct way to do this would be to bind the keydown event on the #inputSize element, not the document!
Indeed it is, but 'ours is not to reason why'... ;)
@adeneo you're right. make it a answer but i guess its too late now... :)
@adeneo, it would be sweet if you showed me what your talking about.
|
1

There is a handler specifically for focus:

$('#inputSize').focus(function () {});

This will call the function passed when the input with the given ID has focus.

5 Comments

For some reason, this still fires after you have given #inputSize focus, but then clicked somewhere else.
did you remove the click handler? Maybe I'm misunderstanding what you're trying to do?
When you say 'this still fires' what do you mean then? You want something to happen when the input gets focus, right?
if there is focus and they press up/down on the keyboard i want the respective alert to come up
ohhh, I see, I will try to modify my answer to address what you want
0

You can test if it's a focused element using document.activeElement as below:

  if($('#inputSize') == document.activeElement) {
      // #inputsize is focused
    }

Note though that most of the time your actions are triggered on another element, so if say you clicked a button then it would have focus and now be the active element, so you couldn't check this with a button check, because the element's already changed by the time your event fired, in most cases.

So , you might end up as

$('#inputSize').click(function(){

      if($('#inputSize') == document.activeElement){
        $(document).keydown(function(e){
            if (e.keyCode == 38) { 
               alert( "up pressed" );
               return false;
            }
                if (e.keyCode == 40) { 
               alert( "down pressed" );
               return false;
            }
        });
    }
});

Comments

0
$('#inputSize').keydown(function(e) {
    if (e.keyCode == 38) 
    { 
       alert( "up pressed" );
       return false;
    }
    if (e.keyCode == 40) 
    { 
       alert( "down pressed" );
       return false;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.