Pre-existing JavaScript for using tab to indent textarea contents?
April 23, 2006 3:58 PM
Subscribe
Has anyone previously published a JavaScript methodology for using the Tab key to indent while within HTML textarea controls?
I'd like to use light markup (Markdown) inside a Web form for composing blog entries/essays/etc. However, many elements require whitespace indentation, e.g. blockquotes, nested lists, etc, for which I'd prefer to use the Tab key instead of raping the spacebar (Markdown works best with 4-space indents, otherwise I might just get by with 2 spaces).
One can use JavaScript to implement a "normal" usage of the Tab key, and I have already started on such an implementation. However, I don't want to re-invent the wheel, and find it hard to believe nobody else has done something like this prior. Google has not helped much (tough thing to search for--far too much noise in the results).
Does anyone know of any pre-existing scripts/techniques that accomplish this, or should I forge ahead and continue writing my own?
Please note that I am familiar with the "tabs vs spaces" debate and do not wish for it to repeat here; in addition, I realize that for publicly facing websites, overriding default keyboard behavior can be an accessibility no-no--this is for the admin interface for a personal website.
posted by cyrusdogstar to computers & internet (13 comments total)
The basic steps would be:
So, something like this:
<textarea id="txtfield" onkeypress="return intercept(event)" />
<script type="javascript" >
function intercept(event) {
var key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (key == 13) {
return " ";
} else {
return key;
}
}
</script>
posted by Civil_Disobedient at 4:28 PM on April 23, 2006