0

I know that many people have asked about tabs and textareas before, and I've seen many good answers, including the code shown below, but for some reason it has no effect on my webpage at all.

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

  if (keyCode == 9) {
    e.preventDefault();
    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;
  }
});

The textarea is the only element on my page at the moment, could that be affecting it? All JS files are linked correctly with my page, and JS is enabled on my browser! Any feedback would be gratefully received!

  • The code is working for me mate. Check this [fiddle](http://jsfiddle.net/UfYXf/) – Harry Sep 07 '13 at 10:56
  • Hmmm. weird. I'm pretty sure I've linked my jquery correctly. Maybe not... it's just this, right? `` –  Sep 07 '13 at 11:16
  • No mate, I don't see a problem with that too. Your problem could be somewhere else. Any errors in the console? – Harry Sep 07 '13 at 11:21
  • I just looked and it gave me this ... `Uncaught ReferenceError: $ is not defined` –  Sep 07 '13 at 11:26
  • Is the `script` tag for jQuery present in the code before the `script` in the question? Check if any of the answers in [this](http://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) SO thread helps. – Harry Sep 07 '13 at 11:30
  • Sure, go for it! I'll accept it when you do :) –  Sep 07 '13 at 11:38

1 Answers1

0

The code given in the question works fine (as can be seen in this fiddle).

The script tag to add jQuery also seems to be fine. But it must be present above the script provided in the question. This is because jQuery is needed for its execution.

Harry
  • 87,580
  • 25
  • 202
  • 214