focus method issue

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

    focus method issue

    Hi, i am designing a WYSIWYG editor. I want that when the user presses
    a 'TAB', a count of four spaces should be put. I used this code,

    <html>
    <head>
    <script>
    function chk(e){
    if (e.keyCode==9){
    c = document.getEle mentById('code' );
    d = c.value ;
    d = d + " ";
    c.value = d ;
    c.focus();
    }
    }
    </script>
    </head>

    <body>
    <textarea id="code" onkeydown="chk( event)" rows=20 cols=60></textarea>
    </body>
    </html>

    However, the focus issue is that, when the user presses the 'TAB',
    then the focus goes to the address bar. How should i avoid it ?
  • Robin

    #2
    Re: focus method issue

    pra__ wrote:
    Hi, i am designing a WYSIWYG editor. I want that when the user presses
    a 'TAB', a count of four spaces should be put. I used this code,
    >
    <html>
    <head>
    <script>
    function chk(e){
    if (e.keyCode==9){
    c = document.getEle mentById('code' );
    d = c.value ;
    d = d + " ";
    c.value = d ;
    c.focus();
    }
    }
    </script>
    </head>
    >
    <body>
    <textarea id="code" onkeydown="chk( event)" rows=20 cols=60></textarea>
    </body>
    </html>
    >
    However, the focus issue is that, when the user presses the 'TAB',
    then the focus goes to the address bar. How should i avoid it ?
    You'll probably (i.e. untested) be wanting to return false (inside the
    if) so that the default TAB behaviour is ignored, and thus don't need to
    c.focus.

    Why do you need the variables 'c' or 'd'? Just:
    document.getEle mentById('code' ).value += " ";
    would replace 4 lines.

    Anyway, the problem is that your code assumes that I'm typing at the end
    of the textarea content. If I'm editing half way down the content and
    want to insert a tab, it just sticks another at the bottom.

    Why reinvent the wheel? A minute with Google reveals several other
    peoples solutions to the "Tabs in TextAreas" issue.

    Robin

    Comment

    • pra__

      #3
      Re: focus method issue

      On Jul 14, 5:08 pm, Robin <a...@somewhere .comwrote:
      pra__ wrote:
      Hi, i am designing a WYSIWYG editor. I want that when the user presses
      a 'TAB', a count of four spaces should be put. I used this code,
      >
      <html>
      <head>
      <script>
      function chk(e){
        if (e.keyCode==9){
            c = document.getEle mentById('code' );
            d = c.value ;
            d = d + "    ";
            c.value = d ;
            c.focus();
        }
      }
      </script>
      </head>
      >
      <body>
      <textarea id="code" onkeydown="chk( event)" rows=20 cols=60></textarea>
      </body>
      </html>
      >
      However, the focus issue is that, when the user presses the 'TAB',
      then the focus goes to the address bar. How should i avoid it ?
      >
      You'll probably (i.e. untested) be wanting to return false (inside the
      if) so that the default TAB behaviour is ignored, and thus don't need to
      c.focus.
      >
      Why do you need the variables 'c' or 'd'? Just:
         document.getEle mentById('code' ).value += "    ";
      would replace 4 lines.
      >
      Anyway, the problem is that your code assumes that I'm typing at the end
      of the textarea content. If I'm editing half way down the content and
      want to insert a tab, it just sticks another at the bottom.
      >
      Why reinvent the wheel? A minute with Google reveals several other
      peoples solutions to the "Tabs in TextAreas" issue.
      >
      Robin
      Thanks Robin,
      I got the idea behind this, and also the phrase, 'reinvent the
      wheel' :-)
      Found this useful link http://ajaxian.com/archives/handling-tabs-in-textareas

      Regards,
      Pranav

      Comment

      Working...