trigger click on button when Enter Key is pressed

S

samuelberthelot

Hi,
This is driving me crazy. In the following code, with e==68 (ASCII code
for 'D'), the button is given the focus and the click is trigered :)
However, if I change to e==13 (ASCII code for Return carriage) then it
never works, and instead the first button in my page is clicked. It's
lilke the Return carriage key is assigned to that one button and
there's nothing that can override this behaviour !!!

My code :

function CheckKey(e){

if( !e ) {
//if the browser did not pass the event information to the
//function, we will have to obtain it from the event register
if( window.event ) {
//Internet Explorer
e = window.event;
} else {
//total failure, we have no way of referencing the event
return;
}
}
if( typeof( e.keyCode ) == 'number' ) {
//DOM
e = e.keyCode;
} else if( typeof( e.which ) == 'number' ) {
//NS 4 compatible
e = e.which;
} else if( typeof( e.charCode ) == 'number' ) {
//also NS 6+, Mozilla 0.9+
e = e.charCode;
} else {
//total failure, we have no way of obtaining the key code
return;
}

if (e == 68){

getEl('ctl00_cpBody_btnSearchPub').focus();
getEl('ctl00_cpBody_btnSearchPub').click();
}
}


Can you help ?

Thanks
 
M

marss

(e-mail address removed) напиÑав:
Hi,
This is driving me crazy. In the following code, with e==68 (ASCII code
for 'D'), the button is given the focus and the click is trigered :)
However, if I change to e==13 (ASCII code for Return carriage) then it
never works, and instead the first button in my page is clicked. It's
lilke the Return carriage key is assigned to that one button and
there's nothing that can override this behaviour !!!

My code :

function CheckKey(e){

if( !e ) {
//if the browser did not pass the event information to the
//function, we will have to obtain it from the event register
if( window.event ) {
//Internet Explorer
e = window.event;
} else {
//total failure, we have no way of referencing the event
return;
}
}
if( typeof( e.keyCode ) == 'number' ) {
//DOM
e = e.keyCode;
} else if( typeof( e.which ) == 'number' ) {
//NS 4 compatible
e = e.which;
} else if( typeof( e.charCode ) == 'number' ) {
//also NS 6+, Mozilla 0.9+
e = e.charCode;
} else {
//total failure, we have no way of obtaining the key code
return;
}

if (e == 68){

getEl('ctl00_cpBody_btnSearchPub').focus();
getEl('ctl00_cpBody_btnSearchPub').click();
}
}


Can you help ?

Thanks

It is not clearly stated and I make some guess-work.
It can occurs if first button has type "submit" and CheckKey is
"onkeypress" or "onkeyup" event handler. If it so that you can either
change first button type to "button" or make CheckKey "onkeydown" event
handler (in the second case you also need to prevent default behavior:
e.g.

if( e.preventDefault )
e.preventDefault();
else
e.cancelBubble = true;

and remove getEl('ctl00_cpBody_btnSearchPub').focus(); - it is
unnecessary).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top