Added $.fn.getCaret()
authorAlexander Ebert <ebert@woltlab.com>
Tue, 28 Aug 2012 13:23:51 +0000 (15:23 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 28 Aug 2012 13:23:51 +0000 (15:23 +0200)
wcfsetup/install/files/js/WCF.js

index 6ed1de6c6264da5767b653d7caf9f34bf1158313..d1b067b80550c2bdf2c208f5372811efbd28add8 100755 (executable)
@@ -283,6 +283,40 @@ $.fn.extend({
                return this.attr('id');
        },
        
+       /**
+        * Returns the caret position of current element. If the element
+        * does not equal input[type=text], input[type=password] or
+        * textarea, -1 is returned.
+        * 
+        * @return      integer
+        */
+       getCaret: function() {
+               if (this.getTagName() == 'input') {
+                       if (this.attr('type') != 'text' && this.attr('type') != 'password') {
+                               return -1;
+                       }
+               }
+               else if (this.getTagName() != 'textarea') {
+                       return -1;
+               }
+               
+               var $position = 0;
+               var $element = this.get(0);
+               if (document.selection) { // IE 8
+                       // set focus to enable caret on this element
+                       this.focus();
+                       
+                       var $selection = document.selection.createRange();
+                       $selection.moveStart('character', -this.val().length);
+                       $position = $selection.text.length;
+               }
+               else if ($element.selectionStart || $element.selectionStart == '0') { // Opera, Chrome, Firefox, Safari, IE 9+
+                       $position = parseInt($element.selectionStart);
+               }
+               
+               return $position;
+       },
+       
        /**
         * Shows an element by sliding and fading it into viewport.
         *