Reputation: 668
I'm this code embed my js file for win key close. This codes:
jQuery(document).keydown(function(e){
alert(e.keyCode); // replace "37" below with the windows key code
if (e.keyCode == 37) {
alert( "windows key pressed" );
return false;
}
});
Working good. But i want only work 1 div. How can i do this?
Upvotes: 0
Views: 103
Reputation: 35572
give an ID to your div. for examle
<div id="testdiv" tabindex="0"></div>
Now write code to bind keydown event to it, on document ready
$(document).ready(function() {
$("#testdiv").keydown(function(e) {
if (e.keyCode == 37) {
alert( "windows key pressed" );
return false;
}
});
});
but remember to assign a tabindex to DIV, it can any number.
Upvotes: 1
Reputation: 20323
Add the selector instead of document
jQuery('selector')
instead of jQuery(document)
where selector can be anything like id, class e.t.c
Demo which uses selector as id
Another Demo will work for only one input inside a div.
Div Demo will work only for first div
Disabled for Div will work only for span not for div
Disabled for input in div will work for all input if they are not inside a div
Upvotes: 1
Reputation: 78991
Use your div selector instead of document
selector.
jQuery("#yourdivid").keydown(function(e){
alert(e.keyCode); // replace "37" below with the windows key code
if (e.keyCode == 37) {
alert( "windows key pressed inside div" );
return false;
}
});
To disable a div, you can simple use $("#yourdivid").prop('disabled', true);
Upvotes: 2
Reputation: 850
You should specify the div you want it to work on:
jQuery("#theIdOfTheDiv").keydown(function(e){
alert(e.keyCode); // replace "37" below with the windows key code
if (e.keyCode == 37) {
alert( "windows key pressed" );
return false;
}
});
Change 'theIdOfTheDiv' with the name of your div.
Upvotes: 1
Reputation: 337560
Change:
jQuery(document).keydown(function(e) {
To
jQuery("#myElement").keydown(function(e) {
Where #myElement
is the id
of your div
.
Upvotes: 3