Javascript beeping an error, but code is working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Scalia

    #1

    Javascript beeping an error, but code is working


    I recently added a small Javascript routine to one of our simple search
    forms. The form has two text input fields, one for a number, the other
    for a name, and it has a button labeled "search". The change I added to
    this form was that if a user hit enter after completing either field, it
    would be just as though the user had pressed the button and the search
    would happen.

    Without the handler, the user's browser just causes their machine to
    beep an error, because, as I'm sure you know, onSubmit doesn't default
    to an enter key with more than one text field present.

    I added the following to each text field definition:

    onKeyPress="che ck_enter(event) "

    the code called is:

    function check_enter(e)
    {
    var char_code;

    if(e && e.which) {
    e = e;
    char_code = e.which;
    } else {
    e = event;
    char_code = e.keyCode;
    }
    if(char_code == 13)
    run_acc_search( );
    else
    return true;
    }

    This is working completely, but the machine is still issuing the same
    error beep when the user presses enter as though an error was made.

    What have I got wrong?


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Thomas 'PointedEars' Lahn

    #2
    Re: Javascript beeping an error, but code is working

    John Scalia wrote:
    [color=blue]
    > Without the handler, the user's browser just causes their machine to
    > beep an error, because, as I'm sure you know, onSubmit doesn't default
    > to an enter key with more than one text field present.[/color]

    I wonder how you got the idea. If I press Enter/Return, the form is
    submitted regardless how many "text fields" are present. Atigs.
    [color=blue]
    > I added the following to each text field definition:
    >
    > onKeyPress="che ck_enter(event) "[/color]

    You might want to consider to handle the `submit' event of the form instead.
    [color=blue]
    > the code called is:
    >
    > function check_enter(e)
    > {
    > var char_code;
    >
    > if(e && e.which) {
    > e = e;[/color]

    The above line is completely useless.
    [color=blue]
    > char_code = e.which;
    > } else {
    > e = event;
    > char_code = e.keyCode;
    > }
    > if(char_code == 13)
    > run_acc_search( );
    > else
    > return true;
    > }
    >
    > This is working completely, but the machine is still issuing the same
    > error beep when the user presses enter as though an error was made.
    >
    > What have I got wrong?[/color]

    You have mixed up your alternatives, and you have not posted the declaration
    of run_acc_search( ). Try this:

    function check_enter(e)
    {
    if (!e) e = window.event;
    if (e)
    {
    if ((e.which || e.keyCode) == 13)
    {
    run_acc_search( );

    /*
    * Use this or let run_acc_search( ) return `false'
    * and then return its return value;
    */
    return false;
    }
    }
    return true;
    }


    HTH

    PointedEars
    --
    186,000 miles per second. It's not just a good idea, it's the law!

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Javascript beeping an error, but code is working

      John Scalia wrote:
      [color=blue]
      > Without the handler, the user's browser just causes their machine to
      > beep an error, because, as I'm sure you know, onSubmit doesn't default
      > to an enter key with more than one text field present.[/color]

      I wonder how you got the idea. If I press Enter/Return, the form is
      submitted regardless how many "text fields" are present. Atigs.
      [color=blue]
      > I added the following to each text field definition:
      >
      > onKeyPress="che ck_enter(event) "[/color]

      You might want to consider to handle the `submit' event of the form instead.
      [color=blue]
      > the code called is:
      >
      > function check_enter(e)
      > {
      > var char_code;
      >
      > if(e && e.which) {
      > e = e;[/color]

      The line above is completely useless.
      [color=blue]
      > char_code = e.which;
      > } else {
      > e = event;
      > char_code = e.keyCode;
      > }
      > if(char_code == 13)
      > run_acc_search( );
      > else
      > return true;
      > }
      >
      > This is working completely, but the machine is still issuing the same
      > error beep when the user presses enter as though an error was made.
      >
      > What have I got wrong?[/color]

      You have mixed up your alternatives, and you have not posted the declaration
      of run_acc_search( ). Try this:

      function check_enter(e)
      {
      if (e && (e.which || e.keyCode) == 13)
      {
      run_acc_search( );

      /*
      * Use this or let run_acc_search( ) return `false'
      * and then return its return value;
      */
      return false;
      }
      return true;
      }


      HTH

      PointedEars
      --
      186,000 miles per second. It's not just a good idea, it's the law!

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Javascript beeping an error, but code is working

        John Scalia wrote:
        [color=blue]
        > Without the handler, the user's browser just causes their machine to
        > beep an error, because, as I'm sure you know, onSubmit doesn't default
        > to an enter key with more than one text field present.[/color]

        I wonder how you got the idea. If I press Enter/Return, the form is
        submitted regardless how many "text fields" are present. Atigs.
        [color=blue]
        > I added the following to each text field definition:
        >
        > onKeyPress="che ck_enter(event) "[/color]

        If you are going to use the result of check_enter(), you have to return its
        return value to the event handler:

        onkeypress="ret urn check_enter(eve nt);"

        However, you might want to consider to handle the `submit' event of the form
        instead.
        [color=blue]
        > the code called is:
        >
        > function check_enter(e)
        > {
        > var char_code;
        >
        > if(e && e.which) {
        > e = e;[/color]

        The line above is completely useless.
        [color=blue]
        > char_code = e.which;
        > } else {
        > e = event;
        > char_code = e.keyCode;
        > }
        > if(char_code == 13)
        > run_acc_search( );
        > else
        > return true;
        > }
        >
        > This is working completely, but the machine is still issuing the same
        > error beep when the user presses enter as though an error was made.
        >
        > What have I got wrong?[/color]

        You have mixed up your alternatives, and you have not posted the declaration
        of run_acc_search( ). Try this:

        function check_enter(e)
        {
        if (e && (e.which || e.keyCode) == 13)
        {
        run_acc_search( );

        /*
        * Use this or let run_acc_search( ) return `false'
        * and then return its return value;
        */
        return false;
        }
        return true;
        }


        HTH

        PointedEars
        --
        186,000 miles per second. It's not just a good idea, it's the law!

        Comment

        • Randy Webb

          #5
          Re: Javascript beeping an error, but code is working

          In a message originally dated November 11, 2004, Thomas 'PointedEars'
          Lahn decided to answer on December 12, 2004 and wrote:
          [color=blue]
          > John Scalia wrote:
          >
          >[color=green]
          >>Without the handler, the user's browser just causes their machine to
          >>beep an error, because, as I'm sure you know, onSubmit doesn't default
          >>to an enter key with more than one text field present.[/color]
          >
          >
          > I wonder how you got the idea. If I press Enter/Return, the form is
          > submitted regardless how many "text fields" are present. Atigs.[/color]

          He didn't say the form didn't submit, he said the onSubmit doesn't
          default to an enter key.

          But is this your way of keeping people from correcting you is by
          answering posts that are over 30 days old? You seem to be an expert at that.

          --
          Randy
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          • Dr John Stockton

            #6
            Re: Javascript beeping an error, but code is working

            JRS: In article <1883098.umQVSm Q8cI@PointedEar s.de>, dated Sun, 12 Dec
            2004 09:09:49, seen in news:comp.lang. javascript, Thomas 'PointedEars'
            Lahn <PointedEars@we b.de> posted :[color=blue]
            >
            >PointedEars
            >--
            >186,000 miles per second. It's not just a good idea, it's the law![/color]

            You are profoundly ignorant. Throughout the modern world, the
            measurement system is by law SI, the International System, and the speed
            of light is 299792458 m/s exactly. And 299792458000/25.4/36/1760 =
            186282.39705122 087011850791378 350433468543704 764177205 ...

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...