event.keyCode=9 tab event doesn't work in FireFox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nasir872
    New Member
    • May 2007
    • 5

    event.keyCode=9 tab event doesn't work in FireFox

    I am having trouble in calling tab event using my own function.
    i call tab event using
    event.keyCode = 9;
    but firefox gives this error "Error: event is not defined".

    What to do? plz help
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    To script event handling beyond IE you need to learn the DOM event model.
    google it, or start here-
    http://www.javascriptk it.com/domref/domevent.shtml

    Comment

    • wgale025
      New Member
      • Feb 2007
      • 23

      #3
      in firefox you can try this:
      [HTML]
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <title>无标题文档</title>
      <script type="text/javascript">
      function codeNum(e)
      {
      alert(e.keyCode );
      }
      </script>
      </head>

      <body>
      <input type="text" onkeydown="code Num(event)"/>
      </body>
      </html>

      [/HTML]

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        actually in firefox guys its charCode not keyCode... keyCode is IE specific.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          First things, first, IE handles events differently from other browsers. In IE, 'event' works because Internet Explorer attaches the last event to the window. So when you type:

          [code=javascript]
          alert(event.key Code);
          [/code]

          You're actually doing this (because 'with window' is assumed in JavaScript):
          [code=javascript]
          alert(window.ev ent.keyCode);
          [/code]

          Instead, you want to use charCode:

          [code=javascript]
          function codeNum(e)
          {
          if(!e)
          e = window.event;
          if(e.keyCode)
          alert(e.keyCode );
          else
          alert(e.charCod e);
          }
          [/code]
          Last edited by pbmods; May 17 '07, 11:21 PM. Reason: Added a 't'. Did I miss any more? Oh, I'm such a "t's"!

          Comment

          • nasir872
            New Member
            • May 2007
            • 5

            #6
            problem is in firefox event.keycode=9 returns error because event.keycode is a getter property. so i can't set tab event by assigning keycode= 9. is there any setter property of event.keycode by which we can assign any keycode to it?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Originally posted by nasir872
              is there any setter property of event.keycode by which we can assign any keycode to it?
              Wait... you want to simulate a tab press?

              Why not just use the focus method of the element you want to focus?

              Comment

              • nasir872
                New Member
                • May 2007
                • 5

                #8
                dear pbmods, my application creates field names on run time so i can't pass the next field name or can't move focus to the field by using its name.

                Comment

                • iam_clint
                  Recognized Expert Top Contributor
                  • Jul 2006
                  • 1207

                  #9
                  and why not? add names to your elements.

                  Comment

                  • ComputerCE
                    New Member
                    • Jun 2007
                    • 1

                    #10
                    Originally posted by iam_clint
                    and why not? add names to your elements.

                    IE provides "event.keyC ode" BUT

                    Mozilla provides "event.whic h"

                    Try this guys..

                    Comment

                    • felipeBoo
                      New Member
                      • Mar 2008
                      • 1

                      #11
                      Originally posted by ComputerCE
                      IE provides "event.keyC ode" BUT

                      Mozilla provides "event.whic h"

                      Try this guys..
                      In Mozilla using "event.which" works with almost all chars, but an example of one that it doesn't works is the TAB char,
                      if your code like this:
                      Code:
                      ........
                      if(event.which){
                          ..........
                      }else{
                          ..........
                      }
                      ........
                      for the char TAB , i'm not sure why, "event.which" is eq 0 so it's evaluated as false!

                      I suggest to use "event.type", it returns the event occured...

                      Comment

                      Working...