Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After hacking around with this a bit, I found the best way was to use the <code>setSelectionRange</code> function if the browser supports it; if not, revert to using the method in Mike Berrow's answer (i.e. replace the value with itself).</p> <p>I'm also setting <code>scrollTop</code> to a high value in case we're in a vertically-scrollable <code>textarea</code>. (Using an arbitrary high value seems more reliable than <code>$(this).height()</code> in Firefox and Chrome.)</p> <p>I've made it is as a jQuery plugin. (If you're not using jQuery I trust you can still get the gist easily enough.)</p> <p>I've tested in IE6, IE7, IE8, Firefox 3.5.5, Google Chrome 3.0, Safari 4.0.4, Opera 10.00.</p> <p>It's available on jquery.com as the <a href="http://plugins.jquery.com/project/PutCursorAtEnd" rel="noreferrer">PutCursorAtEnd plugin</a>. For your convenience, the code for release 1.0 is as follows:</p> <pre><code>// jQuery plugin: PutCursorAtEnd 1.0 // http://plugins.jquery.com/project/PutCursorAtEnd // by teedyay // // Puts the cursor at the end of a textbox/ textarea // codesnippet: 691e18b1-f4f9-41b4-8fe8-bc8ee51b48d4 (function($) { jQuery.fn.putCursorAtEnd = function() { return this.each(function() { $(this).focus() // If this function exists... if (this.setSelectionRange) { // ... then use it // (Doesn't work in IE) // Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh. var len = $(this).val().length * 2; this.setSelectionRange(len, len); } else { // ... otherwise replace the contents with itself // (Doesn't work in Google Chrome) $(this).val($(this).val()); } // Scroll to the bottom, in case we're in a tall textarea // (Necessary for Firefox and Google Chrome) this.scrollTop = 999999; }); }; })(jQuery); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload