[JavaScript] Get element name that has focus

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

[JavaScript] Get element name that has focus

Post 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 :)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: [JavaScript] Get element name that has focus

Post 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:

Code: Select all

$("*")
Then set a handle:

Code: Select all

$("*").focus(function () {
alert(this);
});
I think that'd do the trick. Though, it's untested.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: [JavaScript] Get element name that has focus

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [JavaScript] Get element name that has focus

Post 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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply