Posts Tagged ‘tab’

Articles

Ident Code in Textarea

In jQuery on December 21, 2011 by patrellery Tagged: ,

How many times you wanted to use the tab in your textarea ?

I’m sure a lot!

Here’s a very simple jQuery code that will allow to use the TAB key in a textarea!


$(document).ready(function(e) {

// Using the
$('textarea.tab').keydown(function(e) {
if(e.keyCode == 9) {
var start = $(this).get(0).selectionStart;
$(this).val($(this).val().substring(0, start) + "\t" + $(this).val().substring($(this).get(0).selectionEnd));
$(this).get(0).selectionStart = $(this).get(0).selectionEnd = start + 1;
return false;
}
});

// Using Spaces
$('textarea.space').keydown(function(e) {
if(e.keyCode == 9) {
var start = $(this).get(0).selectionStart;
$(this).val($(this).val().substring(0, start) + " " + $(this).val().substring($(this).get(0).selectionEnd));
$(this).get(0).selectionStart = $(this).get(0).selectionEnd = start + 4;
return false;
}
});
});

View the full article at www.bookofzeus.com/articles/indent-code-in-textarea-using-jquery/