Scrolling textarea to selected text. Pure JavaScript

Really simple task but all solutions I found over internet have some disanvantages.
My also has one: inefficient. But works well in every case and it's written in pure JavaScript.

I stucked it together from all snippets I've seen.

If You know any better solution (without counting chars, pixel, sending keyups and others, not everytime working stuff, please leave comment with code snippet :)

function scrollToPosition(position, textarea) {
  var text = textarea.value;
  var textBeforePosition = text.substr(0, position);
  textarea.value = textBeforePosition;
  textarea.focus();
  textarea.value = text;
  textarea.setSelectionRange(position,position+10);  
  textarea.focus();
} 

Komentarze

Popularne posty