Try listening to keydown and add in <input> val() :
$(function () { $('textarea').keydown(function (e) { var code = e.keyCode || e.which; if (code == '9') { $(this).val($(this).val() + '&nbsb;') return false; } }); });
Key return false; is the key (no pun intended), since the input tab inside the input will in essence advance the user to the next input. It is worth noting that this is the expected behavior, so keep this in mind when introducing this feature to users.
Update: example without jQuery
It occurred to me that you did not mark this question with jQuery, so here is also a solution for vanilla JavaScript:
window.addEventListener('load', function () { document.getElementsByTagName('textarea')[0].addEventListener('keydown', function (e) { var code = e.keyCode || e.which; if (code == '9') { this.value = this.value + '&nbsb;'; e.preventDefault(); } }); }, false);
source share