Share Coding

Tutorials, Problems, Stuffs …

Enable Tab on Textarea

Javascript:

<script type="text/javascript">
	function insertTab(o, e) {
		var kC = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
		if (kC == 9 && !e.shiftKey && !e.ctrlKey && !e.altKey) {
			var oS = o.scrollTop;
			if (o.setSelectionRange) {
				var sS = o.selectionStart;
				var sE = o.selectionEnd;
				o.value = o.value.substring(0, sS) + "\t" + o.value.substr(sE);
				o.setSelectionRange(sS + 1, sS + 1);
				o.focus();
			} else if (o.createTextRange) {
				document.selection.createRange().text = "\t";
				e.returnValue = false;
			}
			o.scrollTop = oS;
			if (e.preventDefault) e.preventDefault();
			return false;
		}
		return true;
	}
</script>

 
Usage:

<textarea onkeydown="insertTab(this, event);"></textarea>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: