/    Sign up×
Community /Pin to ProfileBookmark

Capturing keypress event on div

Hi, I was wondering if there is a better way to capture a key press event on a div. At the moment, I have the following code

[code]
<div onkeypress=”var pressedKeyCode = window.event.keyCode; filter(document.getElementById(‘filterClass’), ‘classList’, 0, 0)”>
foobar
</div>
[/code]

However that only works if I click on the div first. I have many of these divs lying around, they are created using javascript. I dont really want to go down the path of putting the code in the document on keypress, because its normally ends up with headaches using global event handers

Does anyone have any suggestions? Maybe involving the onmouseover() event handler for the div to set the focus to the div, and the onmouseout() to take the focus away again?

Thanks!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Wisest_GuyFeb 02.2006 — [code=html]
<DIV ONMOUSEOVER="this.focus()" ONMOUSEOUT="this.blur()">
[/code]
Copy linkTweet thisAlerts:
@idnauthorFeb 02.2006 — Fantastic! Thanks!
Copy linkTweet thisAlerts:
@KorFeb 02.2006 — clear yourself. Which event you wanna capture? In fact... what in fact is your aim? To get the element upon you do a onclick event? Or what? Why do you need that?
Copy linkTweet thisAlerts:
@idnauthorFeb 02.2006 — No, just to capture the key pressed. I have a table, and want to filter the results, which I have done. I was planning to get the key entered, add it to the text box (which the user can also type in to filter the results) and apply the filter again.

I have changed my approach now however, the previous post have me an idea, why dont I just set the focus to the text box when I mouse over the div. This have one drawback however, that if the user accidently moves the mouse over the div it will take away the focus from another form component, could be kinda annoying.
Copy linkTweet thisAlerts:
@KorFeb 02.2006 — It is not so intricate, but i need to see your table (or a full row of it).
×

Success!

Help @idn 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...