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>
    <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>
  <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>
  <h2>Scope Variables</h2>
    <li>$scope.text1 = "{{text1}}";</li>
    <li>$scope.text2 = "{{text2}}";</li>
    <li>$scope.text3 = "{{text3}}";</li>
              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.";

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

  $scope.insertText = function(text) {
    var input = $scope.lastFocused;
    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") { 
      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") { 
      var range = document.selection.createRange();
      range.moveStart ("character", -input.value.length);
      range.moveStart ("character", pos);
      range.moveEnd ("character", 0);
    else if (browser == "ff") {
      input.selectionStart = pos;
      input.selectionEnd = pos;
    input.scrollTop = scrollPos;

Loading ..................