calling input button click manually

Discussion in 'Javascript' started by CW, Oct 14, 2004.

  1. CW

    CW Guest

    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("netscape") > -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
     
    CW, Oct 14, 2004
    #1
    1. Advertising

  2. CW

    Lee Guest

    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("netscape") > -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?
     
    Lee, Oct 14, 2004
    #2
    1. Advertising

  3. CW

    CW Guest

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

    thanx
    "Lee" <> wrote in message
    news:...
    > 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("netscape") > -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?
    >
     
    CW, Oct 14, 2004
    #3
  4. CW

    Lee Guest

    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.
     
    Lee, Oct 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    881
    Purvi T
    Oct 19, 2004
  2. mike parr
    Replies:
    1
    Views:
    502
    Marina
    May 20, 2004
  3. Ben Fidge
    Replies:
    5
    Views:
    749
    S. Justin Gengo
    Jun 15, 2004
  4. Ryan Ternier
    Replies:
    4
    Views:
    4,021
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 29, 2004
  5. Amy
    Replies:
    0
    Views:
    1,011
Loading...

Share This Page