SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Location
    Philadelphia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Graphic Form Buttons -'dinging'

    We have several forms on our web site. Recently we replaced the standard gray buttons with graphic buttons.

    To make the 'submit' button work when the "Enter" key is pressed (as it does with the standard form buttons), we added the javascript function below. It is called with onKeyPress in the body tag.

    NS4 = (document.layers) ? true : false;
    function checkEnter(event)
    {
    var code = 0;
    if (NS4)
    code = event.which;
    else
    code = event.keyCode;

    if (code==13) { document.forms[0].submit();
    }


    The code works fine for submitting the form. The only problem is that, whenever it is called (that is whenever you try to submit the form by pressing "Enter" when the focus is not already on the button), many of our machines let out the noisy "ding" that usually accompanies a warning message.

    Any ideas on how we can get rid of the noise?

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just made this for you

    PHP Code:
    <html>
      <
    head>
        <
    title></title>

        <
    script type="text/JavaScript">
        <!--

          
    ie = (document.all)?     true false;
          
    ns = (document.layers)?  true false;

          function 
    keyDown(e)
          {
            if (
    ns)        Key=e.which; else Key event.keyCode;
            if (
    Key == 13) { alert('submitting form'); document.forms[0].submit(); }
            
    document.forms[0].childNodes[0].value Key
          
    }

          function 
    handleError()
          {
            return 
    true//this isn't needed but i thought i'd demonstrate suppressing errors.
          
    }

          
    window.onerror handleError// calls error just incase
          
    document.onkeydown keyDown
          
    if (nsdocument.captureEvents(Event.KEYDOWN);
        
    //-->
        
    </script>
      </head>
      <body>
        <form action="#na" method="get">
          <input type="text" name="firstelement">
        </form>
      </body>
    </html> 

  3. #3
    SitePoint Member
    Join Date
    Feb 2002
    Location
    Philadelphia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew,
    Your code works fine for submitting the form --but that was already working, and I still get the dinging sound.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code only makes a noise when submitting the form is a clicking noise.

    PHP Code:
    <html>
      <
    head>
        <
    title></title>

        <
    script type="text/JavaScript">
        <!--

          
    ie = (document.all)?     true false;
          
    ns = (document.layers)?  true false;

          function 
    keyDown(e)
          {
            if (
    ns)        Key=e.which; else Key event.keyCode;
            if (
    Key == 13) { /* alert('submitting form'); */ document.forms[0].submit(); }
            
    document.forms[0].childNodes[0].value Key
          
    }

          function 
    handleError()
          {
            return 
    true//this isn't needed but i thought i'd demonstrate suppressing errors.
          
    }

          
    window.onerror handleError;
          
    document.onkeydown keyDown
          
    if (nsdocument.captureEvents(Event.KEYDOWN);
        
    //-->
        
    </script>
      </head>
      <body>
        <form action="#na" method="get">
          <input type="text" name="firstelement">
        </form>

        <form action="#na" method="get">
          <input type="submit">
        </form>
      </body>
    </html> 

  5. #5
    SitePoint Member
    Join Date
    Feb 2002
    Location
    Philadelphia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Andrew-J2000
    The code only makes a noise when submitting the form is a clicking noise.
    We had that on some machines, but others dinged --something to do with system settings.

    But. . .I did find a workaround. I simply added a normal "Submit" button at the end of the form, then used stylesheets to hide it (by setting height and width to 0px, "display:none" will NOT do it!) Now either clicking on the graphic button (or tabbing to it), or just pressing Enter will work. And. . . I don't need the javascript at all!

    Thanks for your efforts!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •