Skip to content

Instantly share code, notes, and snippets.

@kselax
Last active March 17, 2018 20:03
Show Gist options
  • Select an option

  • Save kselax/84a305ae2f4d5d81350ddb7a7596fed5 to your computer and use it in GitHub Desktop.

Select an option

Save kselax/84a305ae2f4d5d81350ddb7a7596fed5 to your computer and use it in GitHub Desktop.
TextInserter. (javascript)
class TextInserter{
constructor(selector, text){
this.selector=selector
this.text=text;
this.field=$(selector).get(0);
this.value=$(selector).val();
this.start=this.field.selectionStart;
this.finish=this.field.selectionEnd;
this.putText();
}
putText(){
this.value=this.value.substr(0, this.start) + this.text + this.value.substr(this.finish);
$(this.selector).val(this.value);
}
}//TextInserter
var obj=new TextInserter('textarea', 'some text');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment