Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar

.on( "keydown" doens't work but .on( "click" does

$( ".aAU" ).on( "keydown", function() {
  alert('Key pressed'); // no alert displayed when a key is pressed
});


$( ".aAU" ).on( "click", function() {
  alert('clicked'); // alert displayed when a click is done
});
0 likes
7 replies
Snapey's avatar

depends where focus is when you key down

this will only work if an input element of class aAU has focus

ilex01's avatar

@Snapey Thanks for trying to help me.

So I did this:

$(function() {
   $('.aAU').focus();
});
	
$( ".aAU:focus" ).on( "keydown", function() {
  alert('Key pressed'); // no alert displayed when a key is pressed
});

But no alert is displayed when a key is pressed.

Snapey's avatar

@ilex01 what type of element is it?

$(function() {
   $('.aAU').focus();
});

only works if there can only be one element with that class

Bogey's avatar

@ilex01 turn it into an ID '#aAU' rather than it being a class '.aAU' and make sure no other element is sharing that ID.

Please or to participate in this conversation.