Skip to content

Instantly share code, notes, and snippets.

@TerryMooreII
Last active December 31, 2015 04:39
Show Gist options
  • Save TerryMooreII/7935223 to your computer and use it in GitHub Desktop.
Save TerryMooreII/7935223 to your computer and use it in GitHub Desktop.
Add Tab support within a text area. In particular Metlifes Synapse site
//Added tab support to Metlife Synapse json resume builder site
$('#json_textarea').on('keydown', function(event){
$this = $(this);
var TAB_SPACES = 2;
var getSpaces = function(){
var space='';
for(var i=0; i<TAB_SPACES; i++){
space += ' ';
}
return space;
}
if (event.keyCode === 9){
event.preventDefault();
var start = $this.get(0).selectionStart
, end = $this.get(0).selectionEnd
, isShiftTab = false
, beginText = $this.val().substring(0, start)
, endText = $this.val().substring(end)
, spaces = getSpaces();
if (event.shiftKey === true && beginText.substr(beginText.length - TAB_SPACES) === spaces) {
beginText = $this.val().substring(0, start - TAB_SPACES);
isShiftTab = true;
}
else{
beginText += spaces;
}
$this.val(beginText + endText);
$this.get(0).selectionStart =
$this.get(0).selectionEnd = start + (isShiftTab ? -TAB_SPACES : TAB_SPACES);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment