Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Javascript (https://www.askmehelpdesk.com/forumdisplay.php?f=452)
-   -   Textarea cursor position (https://www.askmehelpdesk.com/showthread.php?t=167855)

  • Jan 2, 2008, 05:01 AM
    benn11
    Textarea cursor position
    I have a textarea in an asp page and I have an attribute to insert * symbol in the textarea upon a click in the textarea. Using "<textarea name="text" onfocus="if(this.value=='') this.value='*';">"

    The problem is that the cursor returns to the start position but I want the cursor to remain in the position after the * symbol is automatically inserted. How can this be done?
  • Jan 16, 2008, 09:20 AM
    jstrike
    This should work:

    Code:

    <textarea name="myTextArea" onClick="placeText(this,'*')"></textArea>
                   
    <script>
      function placeText(txtArea, txt) {
        if(txtArea.value!="") return;

        txtArea.value=txt;
        //FF will automatically put the cursor at the end so skip this IE specific part...
        if(navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
          var range = txtArea.createTextRange();
          range.moveStart('character', 1); 
          range.select(); 
        }
      }
    </script>


  • All times are GMT -7. The time now is 07:50 PM.