467,146 Members | 1,078 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

calling input button click manually

CW
In my javascript, when enter key is pressed, I want to simulate the effect
of clicking a button.

var mykey;
var posted=false;

if (window.Event){
document.captureEvents(Event.KEYDOWN);
}

document.onkeydown = myKeyDown;

function myKeyDown(e){

if (window.Event){
mykey = e.which;
}
else{
mykey = event.keyCode;
}
//alert(mykey);

var theform;
if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}

if ((mykey==13) && (posted==false) && (theform.btnSend.click))
{
posted=true;
theform.btnSend.click();
}
}

Everything works fine in IE4 to IE6. But NS behaviour is really bizzare.

Thing work as expected in NS7.

In NS6, theform.btnSend.click() does not cause form submission.

In NS4, it complains that theform.btnSend.click() does not exist (despite
having passed the if test that verifies theform.btnSend.click exists).

Any idea?

Thanks
Jul 23 '05 #1
  • viewed: 5978
Share:
3 Replies
Lee
CW said:

In my javascript, when enter key is pressed, I want to simulate the effect
of clicking a button.

var mykey;
var posted=false;

if (window.Event){
document.captureEvents(Event.KEYDOWN);
}

document.onkeydown = myKeyDown;

function myKeyDown(e){

if (window.Event){
mykey = e.which;
}
else{
mykey = event.keyCode;
}
//alert(mykey);

var theform;
if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}

if ((mykey==13) && (posted==false) && (theform.btnSend.click))
{
posted=true;
theform.btnSend.click();
}
}

Everything works fine in IE4 to IE6. But NS behaviour is really bizzare.

Thing work as expected in NS7.

In NS6, theform.btnSend.click() does not cause form submission.

In NS4, it complains that theform.btnSend.click() does not exist (despite
having passed the if test that verifies theform.btnSend.click exists).

Any idea?


It's generally a bad idea to do anything as drastic as submit a
form on keydown. That leaves the keyup event in limbo, with
unpredictable consequences.

I don't have NS6 installed, but NS4 submits for me when the button's
click() method is called.

Is there a reason why you're calling the click() method, instead of
just directly invoking the form's submit() method?

Jul 23 '05 #2
CW
because there are 2 buttons on the form. If I call submit, it doesn't
specify which button is pressed.

thanx
"Lee" <RE**************@cox.net> wrote in message
news:ck*********@drn.newsguy.com...
CW said:

In my javascript, when enter key is pressed, I want to simulate the effect
of clicking a button.

var mykey;
var posted=false;

if (window.Event){
document.captureEvents(Event.KEYDOWN);
}

document.onkeydown = myKeyDown;

function myKeyDown(e){

if (window.Event){
mykey = e.which;
}
else{
mykey = event.keyCode;
}
//alert(mykey);

var theform;
if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}

if ((mykey==13) && (posted==false) && (theform.btnSend.click))
{
posted=true;
theform.btnSend.click();
}
}

Everything works fine in IE4 to IE6. But NS behaviour is really bizzare.

Thing work as expected in NS7.

In NS6, theform.btnSend.click() does not cause form submission.

In NS4, it complains that theform.btnSend.click() does not exist (despite
having passed the if test that verifies theform.btnSend.click exists).

Any idea?


It's generally a bad idea to do anything as drastic as submit a
form on keydown. That leaves the keyup event in limbo, with
unpredictable consequences.

I don't have NS6 installed, but NS4 submits for me when the button's
click() method is called.

Is there a reason why you're calling the click() method, instead of
just directly invoking the form's submit() method?

Jul 23 '05 #3
Lee
CW said:

because there are 2 buttons on the form. If I call submit, it doesn't
specify which button is pressed.


You can set a hidden form field to tell which was pressed,
or better, redesign it so that it will work without Javascript,
for those who have it disabled.

Please don't top post. Respond *after* the message you're
responding to.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Bill Grigg | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.