Hi I have written this JS script to capture a TAB keypress capture. It captures and insert a \t in the current pointer location in the "pre" tag.
$m = jQuery.noConflict();
$m(document).keydown(function(event) {
var keyCode = event.which;
if(keyCode == 9) {
// alert(); if this is there the code works, but if i remove this the code doesn't work, i dont want to alert user every time he hits TAB
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var $this = $m(this);
var value = $this.val();
// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start)
+ "\t"
+ value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
// prevent the focus lose
event.preventDefault();
}
});
<div class="masterContainer" onclick="setFocusDiv();">
<div class="lined"></div>
<pre id="inputId" class="nonlined" contenteditable="true" onkeyup="setLineNu();" onfocus="setLineNu();"></pre>
</div>
but my problem is that when i insert an alert in the script it works perfectrly and insert the \t but when i remove the alert then it does not work. Also there is a focus event there but the focus is lost after the TAB press.