Use JavaScript to place cursor at end of text in text input element

后端 未结 30 3359
时光说笑
时光说笑 2020-11-22 02:48

What is the best way (and I presume simplest way) to place the cursor at the end of the text in a input text element via JavaScript - after focus has been set to the element

相关标签:
30条回答
  • 2020-11-22 03:33
    <script type="text/javascript">  
        function SetEnd(txt) {  
          if (txt.createTextRange) {  
           //IE  
           var FieldRange = txt.createTextRange();  
           FieldRange.moveStart('character', txt.value.length);  
           FieldRange.collapse();  
           FieldRange.select();  
           }  
          else {  
           //Firefox and Opera  
           txt.focus();  
           var length = txt.value.length;  
           txt.setSelectionRange(length, length);  
          }  
        }   
    </script>  
    

    This function works for me in IE9, Firefox 6.x, and Opera 11.x

    0 讨论(0)
  • 2020-11-22 03:34

    Set the cursor when click on text area to the end of text... Variation of this code is...ALSO works! for Firefox, IE, Safari, Chrome..

    In server-side code:

    txtAddNoteMessage.Attributes.Add("onClick", "sendCursorToEnd('" & txtAddNoteMessage.ClientID & "');")
    

    In Javascript:

    function sendCursorToEnd(obj) {
        var value =  $(obj).val(); //store the value of the element
        var message = "";
        if (value != "") {
            message = value + "\n";
         };
        $(obj).focus().val(message);
        $(obj).unbind();
     }
    
    0 讨论(0)
  • 2020-11-22 03:34

    I also faced same problem. Finally this gonna work for me:

    jQuery.fn.putCursorAtEnd =  = function() {
    
      return this.each(function() {
    
        // Cache references
        var $el = $(this),
            el = this;
    
        // Only focus if input isn't already
        if (!$el.is(":focus")) {
         $el.focus();
        }
    
        // If this function exists... (IE 9+)
        if (el.setSelectionRange) {
    
          // Double the length because Opera is inconsistent about whether a carriage return is one character or two.
          var len = $el.val().length * 2;
    
          // Timeout seems to be required for Blink
          setTimeout(function() {
            el.setSelectionRange(len, len);
          }, 1);
    
        } else {
    
          // As a fallback, replace the contents with itself
          // Doesn't work in Chrome, but Chrome supports setSelectionRange
          $el.val($el.val());
    
        }
    
        // Scroll to the bottom, in case we're in a tall textarea
        // (Necessary for Firefox and Chrome)
        this.scrollTop = 999999;
    
      });
    
    };
    

    This is how we can call this:

    var searchInput = $("#searchInputOrTextarea");
    
    searchInput
      .putCursorAtEnd() // should be chainable
      .on("focus", function() { // could be on any event
        searchInput.putCursorAtEnd()
      });
    

    It's works for me in safari, IE, Chrome, Mozilla. On mobile devices I didn't tried this.

    0 讨论(0)
  • 2020-11-22 03:36

    I like the accepted answer a lot, but it stopped working in Chrome. In Chrome, for the cursor to go to the end, input value needs to change. The solution is as follow:

    <input id="search" type="text" value="mycurrtext" size="30" 
       onfocus="var value = this.value; this.value = null; this.value = value;" name="search"/>
    
    0 讨论(0)
  • 2020-11-22 03:37

    Still the intermediate variable is needed, (see var val=) else the cursor behaves strange, we need it at the end.

    <body onload="document.getElementById('userinput').focus();">
    <form>
    <input id="userinput" onfocus="var val=this.value; this.value=''; this.value= val;"
             class=large type="text" size="10" maxlength="50" value="beans" name="myinput">
    </form>
    </body>
    
    0 讨论(0)
  • 2020-11-22 03:37

    I tried the suggestions before but none worked for me (tested them in Chrome), so I wrote my own code - and it works fine in Firefox, IE, Safari, Chrome...

    In Textarea:

    onfocus() = sendCursorToEnd(this);
    

    In Javascript:

    function sendCursorToEnd(obj) { 
    var value = obj.value; //store the value of the element
    var message = "";
    if (value != "") {
        message = value + "\n";
    };
    $(obj).focus().val(message);}
    
    0 讨论(0)
提交回复
热议问题