473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

trigger click on button when Enter Key is pressed

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

Aug 10 '06 #1
1 8874

sa*************@googlemail.com написав:
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).

Aug 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
<input type="button" onClick="doSomething()"> When the user click HTML button, it will launch doSomething(). But I want the user enter ENTER key, it will have same effect. Please advise....
5
by: csgraham74 | last post by:
Hi there, I have just written an applcation in asp.net. In testing i have found that the enter key press on a button does not fire a javascript confirm prompt. is there anyway i can get this...
20
by: anthonymelillo | last post by:
Is there a way to call a button click event from somewhere else ? I have a text box where I would like to call a button click event when a user presses enter in the text box. Can I do this ?...
12
by: SJ | last post by:
Hope someone can help me out there I'm struggling with a particular problem... I have a form with many tab pages. On one tab page I've got a button which when clicked with a mouse adds items...
3
by: Robert W. | last post by:
I'm new to ASP.net programming so excuse my ignorance if the following question seems overly simplistic. I've created a simple Login form with 3 primary WebControls: - A TextBox for the Username...
2
by: jb | last post by:
Hi! I whould like to lanunch an async postback trigger from an updated panel when a user presses the key return with the cursor in a textbox, is that possible? Thanks, Xus
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
6
by: Mark B | last post by:
I have a function that looks up a SQL table to see if a search term matches. It works fine but so far there are two things yet to work: 1) After entering a search term and pressing Enter, nothing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.