Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > calling input button click manually

Reply
Thread Tools

calling input button click manually

 
 
CW
Guest
Posts: n/a
 
      10-13-2004
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


 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      10-13-2004
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?

 
Reply With Quote
 
 
 
 
CW
Guest
Posts: n/a
 
      10-14-2004
because there are 2 buttons on the form. If I call submit, it doesn't
specify which button is pressed.

thanx
"Lee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      10-14-2004
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.

 
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
How to fire both event button click and textchanged when button is click and text is changed Amy ASP .Net 0 06-01-2006 02:33 PM
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Building Controls 1 05-28-2004 06:23 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net 2 05-25-2004 11:45 PM



Advertisments