Attached code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
/****************************************************************************************
* Project : beije.fi
* Type : random js
* File : inline
****************************************************************************************/
// Bind function to documents onkeydown, you could bind this directly to
// the textarea editormessage, but I've chosen to bind it to document if
// I ever want to expand the listener to multiple textareas or if I want
// to listen after other keyevents I can use the same listener.
document.onkeydown = tablistener;
// If editormessage gets focus or is relieved of its focus, set isfocused accordingly
// (editor message is the textarea)
getbyid('editormessage').onfocus = function() { this.isfocused = true; }
getbyid('editormessage').onblur = function() { this.isfocused = false; }
// Tablistener
function tablistener( e )
{
// Determine correct event
var e = e || window.event;
// Determine what keycode that was entered in the event
var key = e.keyCode || e.charCode;
// if key press is tab (9) and textarea has focus
if( key == 9 && getbyid('editormessage').isfocused == true )
{
// Set text to empty
text = '';
// Retrieve position
position = getbyid('editormessage').selectionStart;
// Retrieve text
text = getbyid('editormessage').value;
// Split text
textarray = text.split('');
// Create text (3 * nbsp)
nbsp = String.fromCharCode(160)+String.fromCharCode(160)+String.fromCharCode(160);
// add spaces to cursor positon
textarray[position] = nbsp+textarray[position];
// join together text
text = textarray.join('');
// Set text back to textarea
getbyid('editormessage').value = text;
// Set cursor infront of spaces
getbyid('editormessage').selectionStart = position+3;
getbyid('editormessage').selectionEnd = position+3;
// Cancel the tab event
return false;
}
// If the event was not a tab and / or it wasn't within the correct
// textarea, let the event bubble on
return true;
}
* Project : beije.fi
* Type : random js
* File : inline
****************************************************************************************/
// Bind function to documents onkeydown, you could bind this directly to
// the textarea editormessage, but I've chosen to bind it to document if
// I ever want to expand the listener to multiple textareas or if I want
// to listen after other keyevents I can use the same listener.
document.onkeydown = tablistener;
// If editormessage gets focus or is relieved of its focus, set isfocused accordingly
// (editor message is the textarea)
getbyid('editormessage').onfocus = function() { this.isfocused = true; }
getbyid('editormessage').onblur = function() { this.isfocused = false; }
// Tablistener
function tablistener( e )
{
// Determine correct event
var e = e || window.event;
// Determine what keycode that was entered in the event
var key = e.keyCode || e.charCode;
// if key press is tab (9) and textarea has focus
if( key == 9 && getbyid('editormessage').isfocused == true )
{
// Set text to empty
text = '';
// Retrieve position
position = getbyid('editormessage').selectionStart;
// Retrieve text
text = getbyid('editormessage').value;
// Split text
textarray = text.split('');
// Create text (3 * nbsp)
nbsp = String.fromCharCode(160)+String.fromCharCode(160)+String.fromCharCode(160);
// add spaces to cursor positon
textarray[position] = nbsp+textarray[position];
// join together text
text = textarray.join('');
// Set text back to textarea
getbyid('editormessage').value = text;
// Set cursor infront of spaces
getbyid('editormessage').selectionStart = position+3;
getbyid('editormessage').selectionEnd = position+3;
// Cancel the tab event
return false;
}
// If the event was not a tab and / or it wasn't within the correct
// textarea, let the event bubble on
return true;
}