/    Sign up×
Community /Pin to ProfileBookmark

Can i find out which control has the focus?

I have a Javascript function that runs on the KeyDown event of a page, i need to be able to determine which control on my form had/has the focus at the time the function executes, is this possible in Javascript?

Its not really appropriate that i bind the function to all the controls on the page/form, it kinda has to run on the KeyDown event of the page itself, but i ant seem to find a way to identify which control had the focus when a key was pressed…

Can anyone help or point me in the right direction?

Thanks in Advance

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJun 01.2004 — <i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
document.onkeydown = new Function("window.status = [color=red]event.srcElement.id[/color];");
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body id="body"&gt;
&lt;form id="form1"&gt;
&lt;p&gt;&lt;input type="text" id="text1" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type="text" id="text2" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type="text" id="text3" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type="button" value="Button" id="button1" /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

As delineated by the red text, the [FONT=courier new]event.srcElement[/FONT] piece is the key. I'm assuming that the elements [FONT=courier new]id[/FONT] would probably be the most useful property for differentiating between the form controls. I hope that's what you were looking for!
Copy linkTweet thisAlerts:
@UnwantedGuestauthorJun 01.2004 — yeah that worked a treat...

Thanks for your speedy response mate ?
×

Success!

Help @UnwantedGuest spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...