Page 1 of 1
[JavaScript] Get element name that has focus
Posted: Sun Feb 10, 2008 2:30 pm
by arjan.top
Is it possible to get element that has focus? (without onfocus an onblur on all elements of course)
jQuery solution would be fine too

Re: [JavaScript] Get element name that has focus
Posted: Sun Feb 10, 2008 8:37 pm
by JellyFish
I think I understand what your saying. You want to get the element that has focus rather then setting a callback to an element for the focus event.
I think in order to do that with jQuery you'd need to select every element:
Then set a handle:
Code: Select all
$("*").focus(function () {
alert(this);
});
I think that'd do the trick. Though, it's untested.
Re: [JavaScript] Get element name that has focus
Posted: Mon Feb 11, 2008 5:42 am
by arjan.top
perfect, but there is one more problem:
if element is inserted with javascript i dont have focus() function on it, is event like ondomchange or something?
Re: [JavaScript] Get element name that has focus
Posted: Mon Feb 11, 2008 11:33 am
by pickle
As far as I know, you'll have to add the .focus listener to each newly added element individually - it won't be caught with the $("*") identifier on $(document).ready().