cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container" ng-app="app" ng-controller="AddWords">
  <h1>Insert Text At Cursor Position In Last Focused Input With AngularJS</h1>
  <form>
    <div class="form-group"><input ng-model="text1" class="form-control" type="text" id="input1" /></div>
    <div class="form-group"><input ng-model="text2" class="form-control" type="text" id="input2" /></div>
    <div class="form-group"><input ng-model="text3" class="form-control" type="text" id="input3" /></div>
    <div class="form-group"><input class="form-control" type="text" id="input4" value="Integer at justo vitae mi laoreet facilisis." /></div>
    <div class="form-group"><input class="form-control" type="text" id="input5" value="Aenean id nulla hendrerit, ultrices enim ac, vestibulum arcu." /></div>
  </form>
  <div class="btn-group">
    <a class="btn btn-default btn-xs" ng-click="insertText('Ä')">Ä</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('ä')">ä</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('Ö')">Ö</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('ö')">ö</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('Ü')">Ü</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('ü')">ü</a>
    <a class="btn btn-default btn-xs" ng-click="insertText('ß')">ß</a>
  </div>
  <h2>Scope Variables</h2>
  <ul>
    <li>$scope.text1 = "{{text1}}";</li>
    <li>$scope.text2 = "{{text2}}";</li>
    <li>$scope.text3 = "{{text3}}";</li>
  </ul>
</div>
            
          
!
            
              app = angular.module("app", []);
app.controller("AddWords", function($scope) {
  $scope.text1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
  $scope.text2 = "Etiam a enim at urna placerat scelerisque vitae at nulla.";
  $scope.text3 = "Morbi a lacus eget dui lacinia semper eget a est.";

  $scope.lastFocused;
  angular.element("input[type='text']").focus(function() {
    $scope.lastFocused = document.activeElement;
  });

  //http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery
  $scope.insertText = function(text) {
    var input = $scope.lastFocused;
    console.log(input);
    if (input == undefined) { return; }
    var scrollPos = input.scrollTop;
    var pos = 0;
    var browser = ((input.selectionStart || input.selectionStart == "0") ? 
                   "ff" : (document.selection ? "ie" : false ) );
    if (browser == "ie") { 
      input.focus();
      var range = document.selection.createRange();
      range.moveStart ("character", -input.value.length);
      pos = range.text.length;
    }
    else if (browser == "ff") { pos = input.selectionStart };

    var front = (input.value).substring(0, pos);  
    var back = (input.value).substring(pos, input.value.length); 
    input.value = front+text+back;
    pos = pos + text.length;
    if (browser == "ie") { 
      input.focus();
      var range = document.selection.createRange();
      range.moveStart ("character", -input.value.length);
      range.moveStart ("character", pos);
      range.moveEnd ("character", 0);
      range.select();
    }
    else if (browser == "ff") {
      input.selectionStart = pos;
      input.selectionEnd = pos;
      input.focus();
    }
    input.scrollTop = scrollPos;
    console.log(angular.element(input).val());
    angular.element(input).trigger('input');
  }

});
            
          
!
999px
Loading ..................

Console