Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > trigger click on button when Enter Key is pressed

Reply
Thread Tools

trigger click on button when Enter Key is pressed

 
 
samuelberthelot@googlemail.com
Guest
Posts: n/a
 
      08-10-2006
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 == 6{

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


Can you help ?

Thanks

 
Reply With Quote
 
 
 
 
marss
Guest
Posts: n/a
 
      08-11-2006

написав:
> 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 == 6{
>
> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter Key H*ll, control enter key submit button BrianDH ASP .Net Web Controls 3 01-12-2005 08:29 PM
button click event when keyboard's enter key is pressed =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 2 12-20-2004 06:42 PM
asp:Button click when Enter is pressed - help Timothy V ASP .Net 1 07-21-2004 06:34 AM
trigger HTML button by enter ENTER key Matt Javascript 1 03-06-2004 07:01 PM
How to get enter key to trigger button click event/postback Yuval Kordov ASP .Net Web Controls 0 01-07-2004 06:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57