From f73ff47d4b7ffef6a4a0e354fe32a58bd368087c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 20 Oct 2012 13:44:02 +0200 Subject: [PATCH] Add setCaret to WCF.js --- wcfsetup/install/files/js/WCF.js | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index a86a5ebcf4..7f9249beec 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -338,6 +338,42 @@ $.fn.extend({ return $position; }, + /** + * Sets the caret position of current element. If the element + * does not equal input[type=text], input[type=password] or + * textarea, false is returned. + * + * @param integer position + * @return boolean + */ + setCaret: function (position) { + if (this.getTagName() == 'input') { + if (this.attr('type') != 'text' && this.attr('type') != 'password') { + return false; + } + } + else if (this.getTagName() != 'textarea') { + return false; + } + + var $element = this.get(0); + + // set focus to enable caret on this element + this.focus(); + if (document.selection) { // IE 8 + var $selection = document.selection.createRange(); + $selection.moveStart('character', position); + $selection.moveEnd('character', 0); + $selection.select(); + } + else if ($element.selectionStart || $element.selectionStart == '0') { // Opera, Chrome, Firefox, Safari, IE 9+ + $element.selectionStart = position; + $element.selectionEnd = position; + } + + return true; + }, + /** * Shows an element by sliding and fading it into viewport. * -- 2.20.1