Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: scroll textarea to given position
    primarykey
    data
    text
    <p>I have a textarea with lots and lots of text:</p> <pre><code>&lt;textarea cols="50" rows="10" id="txt"&gt;lots and lots of text goes here&lt;/textarea&gt; </code></pre> <p>I want to scroll the textarea down, so the user can see 2000-th character. How can I do this using javasctipt/jQuery?</p> <pre><code>$('#txt').scrollToCharNo(2000); // something like this would be great </code></pre> <h3>EDIT (my solution)</h3> <p>Well, I managed to make it work myself. The only way I found, is to create a DIV with the same font and width as the textarea, put a SPAN near the needed character and find the position of that span.</p> <p>I am sure, that somebody might find my solution useful, so i'll paste it here:</p> <pre><code>jQuery.fn.scrollToText = function(search) { // getting given textarea contents var text = $(this).text(); // number of charecter we want to show var charNo = text.indexOf(search); // this SPAN will allow up to determine given charecter position var anch = '&lt;span id="anch"&gt;&lt;/span&gt;'; // inserting it after the character into the text text = text.substring(0, charNo) + anch + text.substring(charNo); // creating a DIV that is an exact copy of textarea var copyDiv = $('&lt;div&gt;&lt;/div&gt;') .append(text.replace(/\n/g, '&lt;br /&gt;')) // making newlines look the same .css('width', $(this).attr('clientWidth')) // width without scrollbar .css('font-size', $(this).css('font-size')) .css('font-family', $(this).css('font-family')) .css('padding', $(this).css('padding')); // inserting new div after textarea - this is needed beacuse .position() wont work on invisible elements copyDiv.insertAfter($(this)); // what is the position on SPAN relative to parent DIV? var pos = copyDiv.find('SPAN#anch').offset().top - copyDiv.find('SPAN#anch').closest('DIV').offset().top; // the text we are interested in should be at the middle of textearea when scrolling is done pos = pos - Math.round($(this).attr('clientHeight') / 2); // now, we know where to scroll! $(this).scrollTop(pos); // no need for DIV anymore copyDiv.remove(); }; $(function (){ $('#scroll_button').click( function() { // scrolling to "FIND ME" using function written above $('#txt').scrollToText('FIND ME'); return false; } ); }); </code></pre> <p>Here is a demo (it works!): <a href="http://jsfiddle.net/KrVJP/" rel="nofollow">http://jsfiddle.net/KrVJP/</a></p> <p>Since none of the answers actually solved the problem, I'll accept experimentX's one: thank you for putting an effort to help me, I appreciate it! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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