03-23-2008, 01:26 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Jan 2008
Posts: 24
Thanks: 13
|
Hi Andres,
this script I found a while ago should help. It's not very nice code and it's also not unobtrusive, but it should work with most browsers.
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) + ' ' + 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=' ';
}
// 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>
|
|
|
|