javascript - angularjs directive to allow tab character -


i trying create angularjs directive allow input of tab character on text field. working partially error not showing correctly. want field required too. here code

.directive('allowtab', function () {         return {             require: 'ngmodel',             link: function(scope, ele, attrs, c) {                 ele.bind('keydown keyup', function(e) {                     var val = this.value;                     if (e.keycode === 9 && e.type === 'keydown') { // tab pressed                          // caret position/selection                         var start = this.selectionstart,                             end = this.selectionend;                          // set textarea value to: text before caret + tab + text after caret                         this.value = val.substring(0, start) + '\t' + val.substring(end);                          // put caret @ right position again                         this.selectionstart = this.selectionend = start + 1;                          c.$setvalidity('allowtab', true);                         // prevent focus lose                         return false;                      }                     else if(e.keycode !== 9 && e.type === 'keyup') {                         if(val === '') {                             c.$setvalidity('allowtab', false);                         }                         else {                             c.$setvalidity('allowtab', true);                         }                     }                 });              }         }     }); 

here jsfiddle: http://jsfiddle.net/36qp9ekl/184/

are trying keep focus in textbox after tab character?

you have use preventdefault() method:

// ...                 // set textarea value to: text before caret + tab + text after caret                 this.value = val.substring(0, start) + '\t' + val.substring(end);                  // put caret @ right position again                 this.selectionstart = this.selectionend = start + 1;                  c.$setvalidity('allowtab', true);                  e.preventdefault();                  // prevent focus lose                 return false;  // ... 

http://jsfiddle.net/36qp9ekl/186/


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -