View Single Post
Old 03-25-2008, 01:58 AM   #5 (permalink)
ETbyrne
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

That's a good find you have there sidisinsane, the only problem is it adds spaces, not tabs. You can easily fix this though by switching out a few lines:

Code:
<html>

<head>

<script type="text/javascript">
/* 
A function to capture a tab keypress in a textarea and insert 4 spaces and NOT change focus originally by Greg Pinero.
Source: http://www.answermysearches.com/here-is-how-to-make-the-tab-work-in-a-textarea-javascript/265/
*/

function enable_tab(e,el)
{
	// 9 is the tab key, except maybe it's 25 in Safari? oh well for them ...
    if(e.keyCode==9)
    {
		// So the scroll won't move after a tabbing
		var oldscroll = el.scrollTop;
		// This doesn't seem to help anything, maybe it helps for IE
        e.returnValue=false;
        // Check if we're in a firefox deal
		if (el.setSelectionRange)
		{
			var pos_to_leave_caret=el.selectionStart+4;
			// Put in the tab
			el.value = el.value.substring(0,el.selectionStart) + "\t" + el.value.substring(el.selectionEnd,el.value.length);
            // There's no easy way to have the focus stay in the textarea, below seems to work though
			setTimeout("var t=document.getElementById('tab'); t.focus(); t.setSelectionRange(" + pos_to_leave_caret + ", " + pos_to_leave_caret + ");", 0);
		}else{
			// IE code, pretty simple really
			document.selection.createRange().text="\t";
		}
		// Put back the scroll
        el.scrollTop = oldscroll;
	}
}
</script>

</head>


<body>

<textarea name="tab" id="tab" onkeydown="enable_tab(event,document.getElementById('tab'))"></textarea>

</body>

</html>
All I did was change the ' ' things to "\t"
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
The Following User Says Thank You to ETbyrne For This Useful Post:
sidisinsane (04-08-2008)