How do you detect which form input has focus using JavaScript or jQuery?
From within a function I want to be able to determine which form input has focus. I’d like to be able to do this in straight JavaScript and/or jQuery.
Advertisement
Answer
I am not sure if this is the most efficient way, but you could try:
JavaScript
x
9
1
var selectedInput = null;
2
$(function() {
3
$('input, textarea, select').focus(function() {
4
selectedInput = this;
5
}).blur(function(){
6
selectedInput = null;
7
});
8
});
9