[JavaScript]textareaでtab

textareaでtab

var addTabKeyEvent = function($textarea){
	$textarea.on('keydown',function(e){
		if(e.keyCode == 9){
			e.preventDefault();
		}else{
			return;
		}
		var $this = $(this);
		var start = this.selectionStart;
		var v = $this.val();
		$this.val(v.substring(0,start) + "\t" + v.substring(this.selectionEnd));
		this.selectionStart = this.selectionEnd = start + 1;
	});
};
addTabKeyEvent($('textarea'));