preserve tabs in textarea when tab key is pressed

If you want to have the text tabbed when pressing tab in textareas rather than it switching fields.

$(document).delegate('textarea', 'keydown', function(e) { 
  var keyCode = e.keyCode || e.which; 

  if (keyCode == 9) { 
    var start = $(this).get(0).selectionStart;
    var end = $(this).get(0).selectionEnd;

    $(this).val($(this).val().substring(0, start)
                + "\t"
                + $(this).val().substring(end));

    $(this).get(0).selectionStart = 
    $(this).get(0).selectionEnd = start + 1;

Other Items in js
Page Views

This page has been viewed 183 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
You want to be the best, you MUST put the long yards in! Nothing comes easy in life so stop wishing and start DOING! So many people would rather bitch and moan than help themselves. Dont be one of those negative drainers, start today, make a small change and keep going forwards with this attitude!