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
I am not sure if this is the most efficient way, but you could try:
var selectedInput = null;
$(function() {
$('input, textarea, select').focus(function() {
selectedInput = this;
}).blur(function(){
selectedInput = null;
});
});
Try
window.getSelection().getRangeAt(0).startContainer
If all you want to do is change the CSS for a particular form field when it gets focus, you could use the CSS ":focus" selector. For compatibility with IE6 which doesn't support this, you could use the IE7 library.