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/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: