1

I am trying to input numbers into a input boxes from buttons. I am having a hard time figuring out how to find which input box has focus. Is there anything to tell me which input box has focus?

3 Answers3

1

Use

var focused =  document.activeElement;
Lloyd Banks
  • 34,109
  • 54
  • 152
  • 238
0

Perhaps create a new selector:

jQuery.expr[':'].focus = function( elem ) {
  return elem === document.activeElement && ( elem.type || elem.href );
};

and test it like this:

if ($("#idOfTextBox").is(":focus")) {
  ...
}

See Using jQuery to test if an input has focus

Community
  • 1
  • 1
Cilan
  • 11,943
  • 3
  • 33
  • 51
0

Not sure if this is what you're trying to do:

It creates a button next to a text field, then you clic on the button and it sets focus to the text field

<form name="myform2">
<input type="text" name="mytextfield2">
<input type="button" name="mybutton" value="Set Focus" OnClick="document.myform2.mytextfield2.focus();">
</form>

Or with javascript:

<script type="text/javascript" language="JavaScript">
document.forms['myform'].elements['mytextfield'].focus();
</script>
Frakcool
  • 10,540
  • 9
  • 46
  • 78