Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var TextareaTabs = new Class({
- Implements: [Options, Events],
- options: {
- character: '\t'
- },
- initialize: function(element, options){
- this.element = document.id(element);
- if (!this.element) return;
- this.setOptions(options);
- var keys = ['keydown', 'keypress'];
- var fn = function(key){
- this[key] = this[key].bind(this);
- };
- keys.each(fn, this);
- this.element.addEvents({
- 'keydown': this.keydown,
- 'keyup': this.keypress
- });
- },
- keydown: function(e){
- if (e.key !== 'tab') return;
- e.preventDefault();
- },
- keypress: function(e){
- if (e.key !== 'tab') return;
- e.preventDefault();
- //IE support
- if (document.selection){
- this.element.focus();
- sel = document.selection.createRange();
- sel.text = this.options.character;
- }
- //MOZILLA/NETSCAPE support
- else if (this.element.selectionStart || this.element.selectionStart == '0') {
- var startPos = this.element.selectionStart;
- var endPos = this.element.selectionEnd;
- restoreTop = this.element.scrollTop;
- this.element.value = this.element.value.substring(0, startPos) + this.options.character + this.element.value.substring(endPos, this.element.value.length);
- this.element.selectionStart = startPos + this.options.character.length;
- this.element.selectionEnd = startPos + this.options.character.length;
- if (restoreTop > 0)
- this.element.scrollTop = restoreTop;
- }
- else {
- this.element.value += this.options.character;
- }
- }
- });
Add Comment
Please, Sign In to add comment